Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket connection breaking frequently with flask-socketio

I am using flask-socketio to make a socket connection from my python web server to the javascript client. I am able to establish the connection but it breaks in a while(5 seconds or so) with the error

socket.io.min.js:2 WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket&sid=8ed663e14c6f47328b64f2d29a30d1cd' failed: Received a broken close frame containing invalid UTF-8.

Server side code to send the message(calling this periodically, say every 5 seconds)

def send_message(result):
    # it will forward the message to all clients.
    print("sending message")
    socketio.send("Send working",  json=False)

Client side code to receive the message

socket.on('message', function (data) {
    console.log('message form backend ' + data);
});

Somehow it breaks -> then nothing happens for a while -> then connect again automatically -> then breaks again.

Can someone please help? Thanks a lot!

like image 838
Vipul J Avatar asked Apr 09 '18 10:04

Vipul J


People also ask

Does Socket.IO use WebSockets?

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.

How much traffic can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

Does Socket.IO use HTTP?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.


1 Answers

I fixed it.

socketio = SocketIO(app,ping_timeout=5)

ping_timeout – The time in seconds that the client waits for the server to respond before disconnecting. So it will be disconnected after 5 seconds if you do nothing.

The solution is that: Make your client side send message to server before timeout.

Cause my server usually sends data, so I make my client side like:

socket.on('message', function (data) {
    console.log('message form backend ' + data);
    socket.send('data receive!');
});
like image 183
Kate Lee Avatar answered Dec 18 '22 16:12

Kate Lee