1 | # Primus Emitter
|
2 |
|
3 | [![Build Status](https://img.shields.io/travis/cayasso/primus-emitter/master.svg)](https://travis-ci.org/cayasso/primus-emitter)
|
4 | [![NPM version](https://img.shields.io/npm/v/primus-emitter.svg)](https://www.npmjs.com/package/primus-emitter)
|
5 | [![Coverage Status](https://img.shields.io/coveralls/cayasso/primus-emitter/master.svg)](https://coveralls.io/r/cayasso/primus-emitter)
|
6 |
|
7 | Node.JS module that adds emitter capabilities to [Primus](https://github.com/primus/primus).
|
8 |
|
9 | ## Installation
|
10 |
|
11 | ```
|
12 | $ npm install primus-emitter
|
13 | ```
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ### On the Server
|
18 |
|
19 | ```javascript
|
20 | var Primus = require('primus');
|
21 | var Emitter = require('primus-emitter');
|
22 | var server = require('http').createServer();
|
23 |
|
24 | // primus instance
|
25 | var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
|
26 |
|
27 | // add emitter to Primus
|
28 | primus.use('emitter', Emitter);
|
29 |
|
30 | primus.on('connection', function (spark) {
|
31 |
|
32 | // emit hi event
|
33 | spark.send('hi', 'good morning');
|
34 |
|
35 | // emit to news with ack
|
36 | spark.send('news', 'good morning', function (data) {
|
37 | console.log(data); // => 'by client'
|
38 | });
|
39 |
|
40 | // receive incoming sport messages
|
41 | spark.on('sport', function (data) {
|
42 | console.log('sport', data); // => ping-pong
|
43 | });
|
44 |
|
45 | });
|
46 |
|
47 | server.listen(8080);
|
48 | ```
|
49 |
|
50 | ### On the Client
|
51 |
|
52 | ```javascript
|
53 | var primus = Primus.connect('ws://localhost:8080');
|
54 |
|
55 | primus.on('open', function () {
|
56 |
|
57 | // receive incoming hi msgs
|
58 | primus.on('hi', function (data) {
|
59 | console.log(data); // => good morning
|
60 | });
|
61 |
|
62 | // respond ack to server
|
63 | primus.on('news', function (data, fn) {
|
64 | fn('by client');
|
65 | });
|
66 |
|
67 | // send message to server
|
68 | primus.send('sport', 'ping-pong');
|
69 |
|
70 | });
|
71 |
|
72 | ```
|
73 |
|
74 | ## API
|
75 |
|
76 | ### spark#send(event, ..., [fn])
|
77 |
|
78 | Send an event to server to client or client to server.
|
79 |
|
80 | ```javascript
|
81 | spark.send('news', 'hi', fn);
|
82 | ```
|
83 |
|
84 | ### spark#on(event, fn)
|
85 |
|
86 | Listen to incoming events.
|
87 |
|
88 | ```javascript
|
89 | spark.on('news', fn);
|
90 | ```
|
91 |
|
92 | ## Run tests
|
93 |
|
94 | ``` bash
|
95 | $ make test
|
96 | ```
|
97 |
|
98 | ## Other plugins
|
99 |
|
100 | * [primus-rooms](https://github.com/cayasso/primus-rooms)
|
101 | * [primus-multiplex](https://github.com/cayasso/primus-multiplex)
|
102 | * [primus-resource](https://github.com/cayasso/primus-resource)
|
103 |
|
104 | ## License
|
105 |
|
106 | (The MIT License)
|
107 |
|
108 | Copyright (c) 2013 Jonathan Brumley <cayasso@gmail.com>
|
109 |
|
110 | Permission is hereby granted, free of charge, to any person obtaining
|
111 | a copy of this software and associated documentation files (the
|
112 | 'Software'), to deal in the Software without restriction, including
|
113 | without limitation the rights to use, copy, modify, merge, publish,
|
114 | distribute, sublicense, and/or sell copies of the Software, and to
|
115 | permit persons to whom the Software is furnished to do so, subject to
|
116 | the following conditions:
|
117 |
|
118 | The above copyright notice and this permission notice shall be
|
119 | included in all copies or substantial portions of the Software.
|
120 |
|
121 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
122 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
123 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
124 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
125 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
126 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
127 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|