node.jsrpcsocket.iojson-rpcdnode

TCP socket + socket.io bidirectional (JSON-)RPC: dnode?


I'm looking for a way to implement a bidirectional RPC in node.js (means that both client and server provide remote-callable procedures, preferable like json-rpc). It should be usable on top of socket.io and TCP/IP sockets for clients. I was about to use nowjs for websockets and provide a seperate API for "normal" sockets.

Now I just found dnode which claims to work with both. "It works over network sockets and even in the browser with socket.io."

I need to pass JSON objects (containing strings) to each other side. The clients will be written in JavaScript (Browser), JavaScript (Appcelerator Titanium), C# and maybe Java (Android), but there's only an implementation for Java. I read the protocol and I think it's not that easy to implement.

Also there is a method name exchange after connection is established which would be quite a overhead in my application, I don't need it as I know what I implemented on the other side (it's not a public api).

Someone has experience with it or know alternatives? I'm not sure if it's the right thing for my purpose, I need to implement CRUD and PUB/SUB.


Solution

  • Use socket.io , it has support for rooms which can be stored memory or a Redis Pub/Sub implementation. You can also namespace your sockets and provide CRUD through events.

    var io = require('socket.io').listen(80);
    
    var someResource = io
      .of('/someResource')
      .on('create', function (socket) {
        createSomeResource()
      })
      .on('read', function(socket) {
        readSomeResource(id, function(){
          io.sockets.in('roomBasedOnSessionId').emit('data', {my:'json'})
        })
      })
    

    Here is a great walkthrough of some of the topics you'll need, including references to sockets and session sharing. http://www.danielbaulig.de/socket-ioexpress/