node.jssocketstream

ss.api.publish.user('someUser Id', content) is not working in socket stream


I'm using socket stream to send the data to the logged in user by using the following code

var ss = require('socketstream'); .... .... ss.api.publish.user('userId', content);

but the ss.api.publish is undefined is what the error i'm receiving.

Where am i going wrong. Please advice.


Solution

  • The API for a publish to a user is:

    Sending to Users Once a user has been authenticated (which basically means their session now includes a value for req.session.userId), you can message the user directly by passing the userId (or an array of IDs) to the first argument of ss.publish.user as so:

    // in a /server/rpc file
    ss.publish.user('fred', 'specialOffer', 'Here is a special offer just for you!');
    

    Important: When a user signs out of your app, you should call req.session.setUserId(null, cb) to prevent the browser from receiving future events addressed to that userId. Note: This command only affects the current session. If the user is logged in via other devices/sessions these will be unaffected.

    The above is taken from the original document describing the socketstream pub/sub api.

    as you can see, you need to supply one more argument than you thought. That is, becasue on the client side you need to subscribe to a message channel in order to get the message. In the above example, you need to do this in your client side code:

    ss.event.on('specialOffer', function(message){
      alert(message);
    });