I'm having lots of problems getting an official node.js socket.io client connected to a python-socketio server. This is the client library I'm using: https://github.com/socketio/socket.io-client
I've tried both python-socketio and flask-socketio, and in both cases, I get log output from the web server and from the connect() handler, but the client never runs its connect() handler or emits any messages.
Flask-SocketIO Server:
from flask import Flask
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@socketio.on('test')
def handle_message(message):
print('received test: ' + message)
@socketio.on('connect')
def test_connect():
print('got a connect')
socketio.emit('my response', {'data': 'Connected'})
if __name__ == '__main__':
socketio.run(app, port=3000)
python-socketio server:
import socketio
import eventlet
from flask import Flask, render_template
sio = socketio.Server()
app = Flask(__name__)
@app.route('/')
def index():
"""Serve the client-side application."""
return render_template('index.html')
@sio.on('connect')
def connect(sid, environ):
print('connect ', sid)
return True
@sio.on('test')
def message(sid, data):
print('looking at test ', data)
@sio.on('disconnect')
def disconnect(sid):
print('disconnect ', sid)
if __name__ == '__main__':
# wrap Flask application with socketio's middleware
app = socketio.Middleware(sio, app)
# deploy as an eventlet WSGI server
eventlet.wsgi.server(eventlet.listen(('', 3000)), app)
Node.js Client:
//client.js
var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000', {reconnect: true});
// Add a connect listener
socket.on('connect', function () {
console.log('Connected!');
socket.emit("test", {"data":"blob"});
});
socket.emit('CH01', 'me', 'test msg');
Node.js Socket.IO Server (works):
//server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function (socket){
console.log('connection made');
socket.on('CH01', function (from, msg) {
console.log('MSG', from, ' saying ', msg);
});
socket.on('test', function (from, msg) {
console.log("we got test");
});
});
http.listen(3000, function () {
console.log('listening on *:3000');
});
There is a bug in the Socket.IO JavasScript client that only exists when using it from node.js (i.e. the same client works fine when used from the browser).
You can read my analysis here: https://github.com/miguelgrinberg/Flask-SocketIO/issues/253#issuecomment-233241257.
The pull request I filed with socketio-client, almost two months ago: https://github.com/socketio/engine.io-client/pull/483.
If you want an easier workaround, disabling long-polling is a good one, just add { transports: ['websocket']} to the connection options.
Thanks so much for the info (and the engine.io PR). Explicitly specifying the transport in the client works for me. Closing this out.
Dear amckinley,
Please can you put a simple python code to connect to Node.js Socket.IO Server (works) wich you have put it.
Most helpful comment
There is a bug in the Socket.IO JavasScript client that only exists when using it from node.js (i.e. the same client works fine when used from the browser).
You can read my analysis here: https://github.com/miguelgrinberg/Flask-SocketIO/issues/253#issuecomment-233241257.
The pull request I filed with socketio-client, almost two months ago: https://github.com/socketio/engine.io-client/pull/483.
If you want an easier workaround, disabling long-polling is a good one, just add
{ transports: ['websocket']}to the connection options.