0

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
jlabrie
  • 3
  • 2

2 Answers2

1

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

  • Thanks Owen - and thanks for socketstream. I was hoping the answer was something like this. A possibly impertinent follow-up - I think I'm going to tackle the project in coffeescript. app.js obviously lives outside the ss cs<>js link/compile ecosystem. Suggested workflow? Should I just build another module in the server api tree and require it somehow? – jlabrie Mar 26 '12 at 16:05
  • Hey jlabrie. Sure you can do that (I would), or change app.js to app.coffee and start your app with 'coffee app.coffee'. Personally I prefer the first option. – socketstream Mar 28 '12 at 13:12
0

Don't know if it complies to the coding standards, but this probably works:

/server/rpc/demo.js

exports.actions = function(req, res, ss) {
    setTimeout(function () {
        ss.publish.all("newServerTime", new Date());
    }, 1000);
}

When reading the docs I think you can abuse actions for pretty much everything, not just RPC responses.

Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
  • Thanks Jan. This is a (much) cleaner way of expressing the test case I put forward. It still left me wondering about the 'proper' home and execution path for server-side code in the SS ecosystem, which is my real concern because I need to run a fair bit of game timing and logic independent of clients. . . – jlabrie Mar 26 '12 at 16:04