UNPKG

3.27 kBMarkdownView Raw
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
7Node.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
20var Primus = require('primus');
21var Emitter = require('primus-emitter');
22var server = require('http').createServer();
23
24// primus instance
25var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
26
27// add emitter to Primus
28primus.use('emitter', Emitter);
29
30primus.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
47server.listen(8080);
48```
49
50### On the Client
51
52```javascript
53var primus = Primus.connect('ws://localhost:8080');
54
55primus.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
78Send an event to server to client or client to server.
79
80```javascript
81spark.send('news', 'hi', fn);
82```
83
84### spark#on(event, fn)
85
86Listen to incoming events.
87
88```javascript
89spark.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
108Copyright (c) 2013 Jonathan Brumley <cayasso@gmail.com>
109
110Permission is hereby granted, free of charge, to any person obtaining
111a copy of this software and associated documentation files (the
112'Software'), to deal in the Software without restriction, including
113without limitation the rights to use, copy, modify, merge, publish,
114distribute, sublicense, and/or sell copies of the Software, and to
115permit persons to whom the Software is furnished to do so, subject to
116the following conditions:
117
118The above copyright notice and this permission notice shall be
119included in all copies or substantial portions of the Software.
120
121THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
122EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
123MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
124IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
125CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
126TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
127SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.