Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Socketstream(0.3) server-side code confusion

Socketstream(0.3) server-side code confusion

Problem

I'm trying to understand exactly where/how I should implement node.js/socketstream server side code that runs independent of client rpc calls. As a simple example I'm trying to push a regular clock update to connected clients using something like this on the server side:

var pushTime = function() {
    d = new Date();
    ss.publish.all('newServerTime', d);
    return;
};

setInterval(pushTime, 1000);

And setting up the client to subscribe to that publish event sorta like this:

ss.event.on('newServerTime', function(time) {
    return $('#serverTime').val(time);
});

Problem: where do I put/execute the server side pushTime function? The docs suggest the /server/rpc tree so I put it in /server/rpc/demo.js but that yields this error:

ReferenceError: ss is not defined

Mind you, I'm not putting the code in the export.actions block; I believe that's only for client rpc calls.

I tried setting ss at the top of the file:

ss = require('socketstream');

but that's gotta be wrong - now the 'publish.all' method doesn't exist.

I tried putting the code at the bottom of app.js, right after the ss.start call. Again that says the publish.all method doesn't exist (maybe not until there's a client attached?). I'm lost. Any help appreciated; hope this was clear.

Problem courtesy of: jlabrie

Solution

Yup, you could put that code in your actions, nothing to stop you, but better to put it in your 'app.js' file.

To access the internal API from app.js (the one that's sent through to /server/rpc action files) use ss.api

Hence you will need to call:

ss.api.publish.all()

from your 'app.js' file.

Apologies this wasn't documented before. I will update the docs shortly.

Owen

Solution courtesy of: socketstream

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Socketstream(0.3) server-side code confusion

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×