What am I doing wrong with Meteor.onConnection(callback) method?


#1

hi guys.

I try to get a return object of Meteor.onConnection(callback) method but do not get nothing.
I can get the callback return in server log like: Meteor.onConnection( console.log(“new DDP connection”) ) ;
but cannot get any return object from the method. Any clue?

thanks.


#2

I wrote something like:

Meteor.onConnection(function(){ 
     Meteor.publish( "connect", function(){ 
        console.log(this.connection) 
     });
});

and I get the connection object of published collection ‘connect’.
Am I correct ?


#3

From the docs:

The callback is called with a single argument, the server-side connection representing the connection from the client. This object contains the following fields:

So your code should look like this:

Meteor.onConnection(function(connection){ 
  console.log(connection) 
});

I’m not quite sure why you are wanting to publish the connection (which is why I’ve removed it), but if you are, you will need to look at the added, changed, removed and ready methods to get your data sent to the client.


#5

Your piece of code was exactly what i need it.
I used the publish because i didn’t know how to get the connection respond. But you gave me the correct solution.
Thank you so much.
Cheers.