Code in Python, communicate in Node.js and Socket.IO, present in HTML Code in Python, communicate in Node.js and Socket.IO, present in HTML python python

Code in Python, communicate in Node.js and Socket.IO, present in HTML


Apache Thrift is a pretty awesome way to write RPC code between all of the major languages. You write a generic Thrift spec declaring the types and services, and then the code generator creates bindings for your desired languages. You can have apis for calling methods between your node and python code bases.

On a more generic low level approach, ZeroMQ is a message queue library that also has support for all of the major languages. With this, you can design your own solution for how you want to communicate (not just purely RPC). It has patterns for request/reply, push/pull, pub/sub, and pair. It gives you enough tools to put together a scalable system of any type.

I have used both and they are great solutions.

Just as a very rough example, the Thrift spec may be something like this. Lets say you want to communicate events from python to node.js, have it processed and get back some response:

myspec.thrift

struct Event {    1: string message; }service RpcService {    string eventOccurred(1:Event e)}

This defines a data structure called Event with a single string member to hold the message. Then a service called RpcService define one function called eventOccured which expects an Event as an argument, and will return a string.

When you generate this code for python and node.js, you can then use the client side code for python and the server side code for node.js

python

from myrpc import RpcService, ttypes# create a connection somewhere in your codeCONN = connect_to_node(port=9000)def someoneClickedSomething():    event = ttypes.Event("Someone Clicked!")    resp = CONN.eventOccurred(event)    print "Got reply:", resp

node.js

// I don't know node.js, so this is just pseudo-codevar thrift = require('thrift');var myrpc = require('myrpc.js');var server = thrift.createServer(myrpc.RpcService, {  eventOccurred: function(event) {    console.log("event occured:", event.message);    success("Event Processed.");  },});server.listen(9000);


You can look at some messaging systems like 0mq http://www.zeromq.org/


I used the library inspired by this question to turn diagnosis.py into a Socket.IO client. This way I can emit the realtime data to the Node.js Socket.IO server:

socketIO.emit('gaze', ...)

And then have it do a socket.broadcast.emit to emit the data to all the Socket.IO clients (browser and diagnosis.py).

RPC is probably the more standard approach for cross-language development but I find it's a bit of an overkill to do that when the goal is to exchange data. It also does not support evented IO out of the box.

Update on Jan 2013 Since socket.broadcast.emit generates a lot of unnecessary traffic, I tried to find a better way of doing this. The solution I came up with is to use namespaces which is supported by the basic python Socket.IO client library I mentioned.

Python

self.mainSocket = SocketIO('localhost', 80)self.gazeSocket = self.mainSocket.connect('/gaze')self.gazeSocket.emit('gaze', ...)

To connect to the gaze namespace.

Node.js

var gaze = io.of('/gaze').on('connection', function (socket) {    socket.on('gaze', function (gdata) {        gaze.emit('gaze', gdata.toString());    });});

This emits the data received only to clients connected to the gaze namespace.

Browser

var socket = io.connect('http://localhost/gaze');socket.on('gaze', function (data) {    console.log(data);});