Anh Tú Mai
Anh Tú Mai

Reputation: 305

How to make communication between Python client and NodeJS server using socket IO?

I am trying to make communication between NodeJS server and Python client process with Socket IO.To get started, I want to send input message from Python process to NodeJs Process, and it can console.log the message.

I followed sample code in https://pypi.org/project/socketIO-client/.

In my app.js (nodejs file):

var express        = require("express");
var app            = express();
var server         = require("http").Server(app);
var io             = require('socket.io')(server);
server.listen(3000,()=>{
    console.log("Succesful");
});
io.on('connection',function(socket){
    //data is the message I wish to receive from python
    socket.on('news',(data)=>{  
    console.log(data);
    });
});

In python script(which dies after I input my data):

from socketIO_client import SocketIO,LoggingNamespace
import logging

while True:
    data = input("Enter your test data here:")
    with SocketIO('localhost', 3000, LoggingNamespace) as socketIO:
        socketIO.emit('news',{data : data})
        socketIO.wait(seconds=1)

The problem: When I try to execute my python code, I get this error after input my data

File "/home/anhtumai/.local/lib/python2.7/site- 
packages/socketIO_client/__init__.py", line 353, in __init__
resource, hurry_interval_in_seconds, **kw)
File "/home/anhtumai/.local/lib/python2.7/site- 
packages/socketIO_client/__init__.py", line 54, in __init__
self._transport
File "/home/anhtumai/.local/lib/python2.7/site- 
packages/socketIO_client/__init__.py", line 62, in _transport
self._engineIO_session = self._get_engineIO_session()
File "/home/anhtumai/.local/lib/python2.7/site- 
packages/socketIO_client/__init__.py", line 76, in 
_get_engineIO_session
transport.recv_packet())
StopIteration'

What do I need to modify in my scripts(both NodeJS and Python)?

Upvotes: 2

Views: 3553

Answers (1)

Anh Tú Mai
Anh Tú Mai

Reputation: 305

Now I found what the problem is . This library https://pypi.org/project/socketIO-client/. is no longer compatible with socketIO 2.0 . To make it work, I just use https://pypi.org/project/socketIO-client-nexus/ instead. Their usage is basically the same. I just change from socketIO_client with socketIO_client_nexus and it works like a charm.

Upvotes: 1

Related Questions