streamy: Directly use Meteor streams with a friendly API
Fork of ferjep:streamy — modernized for Meteor 3.0, with
underscoreremoved and native JS throughout.
Installation
meteor add a4xrbj1:streamy
Or via npm (GitHub Packages):
npm install @a4xrbj1/streamy --registry=https://npm.pkg.github.com
Note: Meteor keeps logging warnings with _debug about messages not being recognized. You can override Meteor._debug to suppress these warnings (as shown in the example).
Basic Usage
1// Attach an handler for a specific message 2Streamy.on('hello', function(d, s) { 3 console.log(d.data); // Will print 'world!' 4 5 // On the server side only, the parameter 's' is the socket which sends the message, you can use it to reply to the client, see below 6}); 7 8// Send a message 9// from client to server 10Streamy.emit('hello', { data: 'world!' }); 11 12// from server to client, you need an instance of the client socket (retrieved inside an 'on' callback or via `Streamy.sockets(sid)`) 13Streamy.emit('hello', { data: 'world!' }, s);
Core
Streamy.emit(message_name, data_object, [socket])
Send a message with associated data to a socket. On the client, you do not need to provide the socket arg since it will use the client socket. On the server, you must provide it.
Streamy.on(message_name, callback)
Register a callback for a specific message. The callback will be called when a message of this type has been received. Callback are of the form:
1// Client 2Streamy.on('my_message', function(data) { 3 console.log(data); 4}); 5 6// Server 7Streamy.on('my_message', function(data, from) { 8 // from is a Socket object 9 Streamy.emit('pong', {}, from); // An example of replying to a message 10});
Streamy.off(message_name)
Un-register handler of a specific message.
Streamy.onConnect(callback) / Streamy.onDisconnect(callback)
Register callbacks to be called upon connection, disconnection. Please note that this is tied to the websockets only and has nothing to do with authentification.
The callback is parameterless on client. On the server, it will contains one parameter, the socket which has been connected/disconnected.
Multiple servers support
Streamy comes with support for communication between multiple Meteor servers. When your app needs to connect to another Meteor server, if that server uses Streamy then communication by messages can be done easily just like between your server and client.
Streamy.Connection(connection)
This is a constructor to create a Streamy connection. It requires one variable which is the connection to another Meteor server. This connection is the returned value when you connect to other Meteor server by DDP.connect. Instances of this constructor will be able to use these methods: .on, .emit, .off, .onConnect, .onDisconnect. They have the same effect with method having the same name of the Streamy object. Example:
1var connection = DDP.connect('localhost:4000'); 2 3var streamyConnection = new Streamy.Connection(connection); 4 5// call when connect to localhost:4000 success 6streamyConnection.onConnect(function() { 7 console.log('Connected to localhost:4000'); 8 9 // send a message to localhost:4000 10 streamyConnection.emit('hello', { 11 data: 'world', 12 }); 13}); 14 15// listen for message from localhost:4000 16streamyConnection.on('data', function(data) { 17 // ... 18}); 19 20streamyConnection.onDisconnect(function() { 21 console.log('Disconnected from localhost:4000'); 22});
Streamy.Connection.prototype.on(message_name, callback)
Same as Streamy.on
Streamy.Connection.prototype.off(message_name)
Same as Streamy.off
Streamy.Connection.prototype.onConnect(callback) / Streamy.Connection.prototype.onDisconnect(callback)
Same as Streamy.onConnect/Streamy.onDisconnect
Utilities
Streamy.sockets([sid]) Server-only
If no parameter is given, returns all connected socket objects. If a string or an array of strings is provided it will returns a special object with a send method and matched sockets in _sockets.
Streamy.id([socket])
Retrieve the connection id. A unique identifier for each connections. On the server, you should provide the socket object to retrieve the associated connection id.