Flask-socketio: Flask and SocketIO net::ERR_EMPTY_RESPONSE

Created on 28 May 2019  路  4Comments  路  Source: miguelgrinberg/Flask-SocketIO

Hello
I want to create a web app with Flask using SocketIO. The webserver is running on a remote linux environment with the ip address 192.168.0.177. I write this post because i haven't been able to send data from the server to the client.
For testing i have at the Client side a function that send "I am connected" when it goes connected with the server and the server print this string in the terminal. This is working fine, but i also have a Thread in the server side that should send every 1 s a string to the Client and this is not working... dont know why.

This is what i am doing at the server side:

from flask import Flask, render_template, url_for
from flask_socketio import SocketIO, emit, send
import threading
import time

app = Flask(__name__)
app.debug = True
app.host='0.0.0.0'

app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

def send_values():
    while 1:
          socketio.send('new_values', {'data': "string1"})
         #socketio.emit('new_values', {'data': "string2"})
          time.sleep(1)

@socketio.on('my event')
def handle_my_custom_event(json):
    print('received json: ' + str(json))

@app.route('/')
def index():
    return render_template("home.html")

y = threading.Thread(target=send_values, args=())

y.start()

if __name__ == "__main__":
    socketio.run(app)

this is at the Client side:

<script type="text/javascript" charset="utf-8">
    var socket = io();
        socket.on('connect', function() {
        socket.emit('my event', {data: 'I\'m connected!'});
    });
</script>
<script type="text/javascript" charset="utf-8">
    $(function(){
        var socket = io();
        socket.on('new_values', function(msg) {
        $('#test').append('<li>Received: ' + msg.data + '</li>');
        console.log(msg.data)
        });
    });
</script>

I am running the app with the command:
export FLASK_APP=app.py
python3 -m flask run --host=0.0.0.0
After this command i can see my app when i go to 192.168.0.177:5000 so the app is running but i am not able to send data from the server to the Client.

I also become this message at the terminal when i run the command from above:

 * Serving Flask app "app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
[2019-05-28 15:02:22,526] WARNING in __init__: Flask-SocketIO is Running under Werkzeug, WebSocket is not available.

I know there ist this "WebSocket is not available" but is this the problem? I already installed eventlet and gevent.

I am using python3.5.3
jquery 3.4.1
socket 2.2.0

At the browser console i cannot see any missing modules or errors

question

All 4 comments

The send_values() function runs once and then it ends. You probably want a while loop there to repeat the emits. Also it's odd that you are calling send() and emit() there, just pick one.

@miguelgrinberg i changed what you suggested but it still doesn't work. Any other suggestions?
In the Terminal i can see this:

192.168.0.178 - - [28/May/2019 16:04:10] "GET /socket.io/?EIO=3&transport=polling&t=Mh-mOth&sid=621054831da047c8a5b1caa97396f9e4 HTTP/1.1" 200 -
192.168.0.178 - - [28/May/2019 16:04:11] "GET /socket.io/?EIO=3&transport=polling&t=Mh-mP7S&sid=9bc68b592b0e454ea054a3d0ff74c482 HTTP/1.1" 200 -
192.168.0.178 - - [28/May/2019 16:04:11] "GET /socket.io/?EIO=3&transport=polling&t=Mh-mP8A&sid=621054831da047c8a5b1caa97396f9e4 HTTP/1.1" 200 -
192.168.0.178 - - [28/May/2019 16:04:12] "GET /socket.io/?EIO=3&transport=polling&t=Mh-mPN4&sid=9bc68b592b0e454ea054a3d0ff74c482 HTTP/1.1" 200 -
192.168.0.178 - - [28/May/2019 16:04:12] "GET /socket.io/?EIO=3&transport=polling&t=Mh-mPMv&sid=621054831da047c8a5b1caa97396f9e4 HTTP/1.1" 200 -

You are using send(), which uses a predefined event name message. If you want to use a custom event name, then you have to use emit().

Thanks! it works! 馃憤

Was this page helpful?
0 / 5 - 0 ratings