Flask-socketio: SocketIO.run does not work on anything but the default port

Created on 17 Feb 2017  路  2Comments  路  Source: miguelgrinberg/Flask-SocketIO

I've been trying to get a basic Flask Socket IO example to work for a while but for some reason the socket appeared to drop out half way through the connection process.

This is my flask server code:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)
socketio = SocketIO(app)

@app.route('/')
def hello_world():
    logger.info('Serving root')
    return render_template('index.html')

@socketio.on('my event')
def respondToRequest(message):
    logger.info('responding to request')
    emit('my response',{'data':'some response'})

if __name__ == '__main__':
    logger.info('Running socket IO')
    socketio.run(app, host='localhost', port=10001)

This is my template:

<!DOCTYPE html>
<html>
  <head>
    <title>Flask Websockets Demo</title>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.min.js"></script>
    <script type="text/javascript" src="static/js/application.js"></script>
  </head>
  <body>
    <p>Click me</p>
  </body>
</html>

This is the application js:

$(document).ready(function() {
    connectionString = 'http://' + location.host
    console.log('Connecting web socket: '+connectionString)
    ws = io.connect(connectionString);

    console.log('Registering function clicks')
    $( "p" ).on( "click", function() {
        ws.send('request', {who: $(this).attr('id'), data: $(this).val()});
    });
});

When I run the server up I get this:

C:\Anaconda3\python.exe C:/Projects/Experiments/FlaskSocketIoExperiments/FlaskSocketIoExperiments.py
INFO:engineio:Server initialized for gevent.
INFO:__main__:Running socket IO

When I attempt to connect a client I get repeats of this on the server:

INFO:geventwebsocket.handler:127.0.0.1 - - [2017-02-17 09:33:49] "GET / HTTP/1.1" 200 612 0.005000
INFO:engineio:5aa0b8a12c054b34b5ca30407ba3b33c: Sending packet OPEN data {'sid': '5aa0b8a12c054b34b5ca30407ba3b33c', 'pingTimeout': 60000, 'pingInterval': 25000, 'upgrades': ['websocket']}
INFO:engineio:5aa0b8a12c054b34b5ca30407ba3b33c: Sending packet MESSAGE data 0
INFO:geventwebsocket.handler:127.0.0.1 - - [2017-02-17 09:33:49] "GET /socket.io/?EIO=3&transport=polling&t=LfBQw7Z HTTP/1.1" 200 357 0.000000
INFO:engineio:5aa0b8a12c054b34b5ca30407ba3b33c: Received packet MESSAGE data 0:10001
INFO:geventwebsocket.handler:127.0.0.1 - - [2017-02-17 09:33:49] "POST /socket.io/?EIO=3&transport=polling&t=LfBQw8J&sid=5aa0b8a12c054b34b5ca30407ba3b33c HTTP/1.1" 400 215 0.001000
INFO:engineio:5aa0b8a12c054b34b5ca30407ba3b33c: Received packet CLOSE data None
INFO:geventwebsocket.handler:127.0.0.1 - - [2017-02-17 09:33:49] "POST /socket.io/?EIO=3&transport=polling&t=LfBQwBY&sid=5aa0b8a12c054b34b5ca30407ba3b33c HTTP/1.1" 400 215 0.001000

..and this on browser console (Chrome):

Connecting web socket
application.js:6 Registering function clicks
http://localhost:10001/socket.io/?EIO=3&transport=polling&t=LfBQw8J&sid=5aa0b8a12c054b34b5ca30407ba3b33c Failed to load resource: the server responded with a status of 400 (BAD REQUEST)
localhost/:1 WebSocket connection to 'ws://localhost:10001/socket.io/?EIO=3&transport=websocket&sid=5aa0b8a12c054b34b5ca30407ba3b33c' failed: WebSocket is closed before the connection is established.

If I change the server run method from this:

socketio.run(app, host='localhost', port=10001)

..to this:

socketio.run(app, host='localhost', port=5000)

...it appears to work as expected. With the default port I get a trace like this on the server:

C:\Anaconda3\python.exe C:/Projects/Experiments/FlaskSocketIoExperiments/FlaskSocketIoExperiments.py
INFO:engineio:Server initialized for gevent.
INFO:__main__:Running socket IO
INFO:engineio:d27266d267674b1692d11c97a857a5be: Sending packet OPEN data {'upgrades': ['websocket'], 'pingTimeout': 60000, 'sid': 'd27266d267674b1692d11c97a857a5be', 'pingInterval': 25000}
INFO:engineio:d27266d267674b1692d11c97a857a5be: Sending packet MESSAGE data 0
INFO:geventwebsocket.handler:127.0.0.1 - - [2017-02-17 11:18:46] "GET /socket.io/?EIO=3&transport=polling&t=1487330326586-2 HTTP/1.1" 200 357 0.001000
INFO:engineio:d27266d267674b1692d11c97a857a5be: Received request to upgrade to websocket
INFO:engineio:d27266d267674b1692d11c97a857a5be: Sending packet NOOP data None
INFO:geventwebsocket.handler:127.0.0.1 - - [2017-02-17 11:18:47] "GET /socket.io/?EIO=3&transport=polling&t=1487330326899-3&sid=d27266d267674b1692d11c97a857a5be HTTP/1.1" 200 191 0.303017
INFO:engineio:d27266d267674b1692d11c97a857a5be: Upgrade to websocket successful
question

Most helpful comment

Miguel, I must apologise. I'm sure that wasn't working on Friday and yet it works perfectly for me too.

All 2 comments

I can't reproduce this here. I took the example app in this repository, changed it to port 10001 like you did, and everything works for me.

Can you try this example? Let me know if my example works fine on port 10001.

Miguel, I must apologise. I'm sure that wasn't working on Friday and yet it works perfectly for me too.

Was this page helpful?
0 / 5 - 0 ratings