joeytwiddle
joeytwiddle

Reputation: 31305

It is possible to piggy-back on the now.js IO socket?

I am using NowJS but thinking Socket.IO might be more efficient for the very high frequency messages (perhaps less overhead than a nowjs function call).

I wondered if it might be possible to "hijack" the IO socket that Now has already established and multiplex with it, hopefully allowing for a reasonable comparison of the two.

I tried the following:

// On the server
user.socket.on("my_update", receiveUpdate);

// On the client
now.core.socketio.emit("my_update", { hello: "world" } );

but the server event was never triggered.

My Question: How can I use the io socket that NowJS is using? Or can't I?

For extra points, how much faster is it to use socket.io emit() than a function call proxied by NowJS?

Upvotes: 2

Views: 271

Answers (1)

joeytwiddle
joeytwiddle

Reputation: 31305

Yes it is possible.

I was just having trouble finding the right user object containing his socket. Turns out the user object is passed as the context that events are called with:

// On the server
nowjs.on("connect", function(){
  this.socket.on("my_update", receiveUpdate);
  // ( user === this )
});

Messages emitted over the socket seem to be interleaving fine with those from Now itself.

For what it's worth, I collected a list of existing emit message from the NowJS source. These should be considered reserved event names!

connect, del, disconnect, groupdel, grouprv, join, leave, multicall, newgroup,
rd, removegroup, rfc, rv

I have not yet tested for any difference in performance, so that part of the question is still open...

Upvotes: 1

Related Questions