Node.js and Python – Web App Integration with Sockets

node.jspython

Right now I have a webpage with various buttons and options that I'd like to use to send commands to a python script running on the webserver. The python script is being used to interface with devices over a serial connection as well as performing computations with the data received from both the webpage and the serial connection.

I was recommended to use Node.js with socketIO to open up a socket from the front end webpage to the server. I was able to get socketio running in minutes and sending data to a javascript server. Now I tried using that server to replace my python script, but my computations were taking too long, and being single threaded was very limiting.

I'd like to revert back to my python script for handling computations(and possibly serial communications as well), but there are so many options available I don't have time to try them all and see which works best.

As of right now I'm looking at:

  • Javascript websockets talking directly to the python script over socket

    The limitation here is that I won't be taking advantage of the scalability and power of Node.js or socket.io which would handle concurrent connections better without me having to reinvent the wheel.

  • Socket.io talking to a javascript socket.io server, which then relays the data over a net.Socket to the python script

    Is it overkill to have that additional script just relaying information though? In my mind it would be taking advantage of all the socket.io features, but does add a layer to my web stack.

  • Socket.io talking directly to a python script using python-socketio

    I think this makes the most sense, don't really know of any shortfalls.

Ideally the solution would have low latency, allow for data to be sent and received from the front end webpage, and allow for threading so long computations won't block processes. I do NOT need python to act as a websever, and many packages I see are being used that way. Curious as to any input you guys might have.

Best Answer

In the interest of making information available to anyone in the future, my current solution is having a socket.io server running, which connects over a socket to a python script. the socket.io server just relays information between the two and is capable of handling multiple clients whereas a python only solution would not handle multiple clients without me having to reinvent the wheel. So I'm leveraging the multithread power of python with the multi connections of socket.io, without having to recreate what has already been created by others. I didn't go with python-socketio because information was limited and all the documentation involved flask, which I'm not using (I wanna keep my python script as simple and lightweight so that others can easily use it).

Related Topic