arunoda / meteor-streams

Realtime messaging for Meteor
http://arunoda.github.io/meteor-streams
MIT License
287 stars 97 forks source link

onConnected hook #17

Open broth-eu opened 10 years ago

broth-eu commented 10 years ago

It would be nice if I can register on a stream to get notified after a client has established a connection to the server.

In my current project I need this to initialize a context object for each client on the server. Hence, I have to check in every listening (server) method if such a context is already available. In case not, a new context object is created.

spastai commented 10 years ago

Meteor.onConnection(function(connection) {}) could be used here as meteor-streams are using Meteor 'connection'. However here you will face the problem this Meteor.onConnection happens earlier than client streams starts listen for the events (in other words does https://github.com/arunoda/meteor-streams/blob/master/lib/client.js#L33), so you need to modify https://github.com/arunoda/meteor-streams/blob/master/lib/server.js#L38 adding hook at the end of the publish method, something like self.onConnected && self.onConnected(subscriptionId); Then your code could look like:

Meteor.onConnection(function(connection) {
    stream.onConnected = function(id) {
        stream.emit('connect', connection.id);
    }
});

and client

stream.on('connect', function (id) {
))

could do your client initialization