0

I've got a simple nodejs server that runs socketio and I can do 2 way communication with the client from the HTML. Now I'm also trying to connect to the same nodejs websocket from a Python script, but not getting connected.

Simplified Node JS webserver:

var http = require('http').createServer(handler);
var fs = require('fs');
var io = require('socket.io')(http)
http.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/public/index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}

io.sockets.on('connection', function (socket) {
  setInterval(() => {
    socket.emit('temp', 'tempfromserver')
  }, 1000)
});

The Python script that wants to connect to the nodejs with websocket:

import socketio
sio = socketio.Client();
sio.connect('http://localhost:8080')

@sio.event
def message(data):
    print('received message')
def connect():
    print('socketio connected')

Is it possible to connect with a websocket via socketio to the nodejs server? If so, what am I doing wrong?

End goal is to collect sensor data from python script on raspberry pi and send to nodejs server which in its turn saves it to DB and sends through to HTMl client. Perhaps there is a better setup to do this.

1
  • I'm not familiar with python, i assume that sio.connect('http://localhost:8080') should be sio.connect('ws://localhost:8080') Commented Jun 5, 2019 at 1:38

1 Answer 1

0

I believe I asked a similar question and got an answer. My setup was a little different, and used a web-socket, but it will at least allow you to connect between Node JS and Python, and then maybe you can build from there, Using Socket IO and aiohttp for data transfer between node JS and Python. The setup was also originally based off a html script on the node JS side, so that may help as well in comparing it to your setup.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.