7

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!

Vipul J
  • 6,641
  • 9
  • 46
  • 61
  • Does it happen with multiple browsers? Have you tried a different computer? – Miguel Grinberg Apr 11 '18 at 15:49
  • 1
    Have you changed the ping timeout value using something like `socketio = SocketIO(app, ping_timeout=#)`? I believe I came across that exact problem when I changed the ping_timeout to 10 seconds, but didn't change ping_interval, which by default is 25 seconds. If this is your problem, changing ping_interval to be less than the timeout should fix it - it seems to have for me. – Tal Apr 11 '18 at 17:48
  • I tried this but in vain. – hasherBaba Jul 06 '18 at 06:41
  • What does your `socketio` object refer to? – Milan Velebit Aug 03 '18 at 09:11
  • As @Tal noted when I fix timeout/interval numbers I don't get the error... BUT this only prevents the connection from being interrupted, but doesn't explain why I get that invalid UTF-8 content in the close frame? I get the same error when in the front-end user opens printing popup and it stays open for a longer time than my timeout because while that printing popup is open the browser stops sending requests to the back-end, so socketio server thinks that the user is gone and I get that error... – Ruben Kostandyan Dec 24 '19 at 13:01

1 Answers1

1

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!');
});
Kate Lee
  • 11
  • 1