Skip to content

Interoperability with node.js socket.io client #47

@amckinley

Description

@amckinley

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');
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions