0

I'm trying to create an app that uses SocketIO through the python flask-socketio package. I have set up my server as such:

File ./run.py

from my_project import app, socketio

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', debug=True)

File ./my_project/__init__.py

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'MY SECRET KEY'
socketio = SocketIO(app)

@socketio.on('connect')
def print_connect():
    print('Client connected')

@socketio.on('disconnect')
def print_disconnect():
    print('Client disconnected')

from my_project import views

I was getting "not defined" errors when trying to use it on the client, so I opened the socket.io.js file that Chrome had retrieved from the server, only to be surprised by the following contents (in their entirety):

�0{"pingTimeout":60000,"sid":"3d6073ff436c49b1aa06090dbb001ceb","upgrades":[],"pingInterval":25000}�40

To double-check that it had nothing to do with my app, I retrieved the file through curl with the command curl 127.0.0.1:5000/socket.io/socket.io.js which returned the exact same garbled string.

I am not using Express in any fashion (as far as I know), so most of the other questions about sourcing the client socket.io file do not seem to apply. Further, I'm not getting a 404 from the server, I'm getting a 200 and data of some sort.

What can I do to troubleshoot why my server is serving up an obviously corrupted socket.io file?

Christopher Sheaf
  • 185
  • 1
  • 3
  • 13

1 Answers1

3

The Socket.IO JavaScript client needs to be served as a regular static file. Not sure why you expected the /socket.io/socket.io.js path would serve the client, that is not how it works, and in fact, the server has really nothing to do with the JS client.

So you need to change the URL that you use in the <script> tag to point to an actual client library. For example, you can load the client from a CDN at https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js, or if you prefer, download this file, put it in your application's static folder and then load it from there with url_for('static', filename='socket.io.js').

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • I see where the problem is now. I've been following the official SocketIO [get started](https://socket.io/get-started/chat/) docs, where it says to use that magic path string on the client (also explained in [this SO question](https://stackoverflow.com/questions/26480991/how-nodejs-and-socket-io-serve-socket-io-js?rq=1)). It didn't even occur to me until now that the magic path might only exist for the official server-side node.js package, and not for your python bindings. I was worried something was corrupt with my installation. Still seems weird to me that I didn't just get a 404, though. – Christopher Sheaf Nov 06 '17 at 19:48
  • Yes, the Python server is actually not bindings, it is a full server that shares no code with the Node.js official implementation. I think I can improve the parsing of the URL and return a 404 for this case, but right now anything that starts with `/socket.io/` is assumed to be the polling/websocket endpoint for the service. – Miguel Grinberg Nov 07 '17 at 18:47