Trouble connecting to the real time API

Hello there,

I am able to use the REST API smoothly to find games and stuff, but have trouble using to the Real Time API. I initially had the same problem as @the747beast in Opening a SocketIO object with https results in hang. I first tried using the socketIO-client for python like in the post, but then i was getting the same error: unexpected status code (400 {"code":0,"message":"Transport unknown"}).
As pointed out in the previous post, the problem is that the client is asking for / while only websocket is supported. I wasn’t able to find a way to tell SocketIO to use websocket instead of polling, so as a ugly workaround I modified myself the package SocketIO-Client (replacing polling transport with websocket) and managed to finally connect… more or less.

I’m using this simple code:
with SocketIO('') as sio: ...

and sometimes it connects, sometimes I get a Handshake status 502.

Any clues?

Note: I also tried the websocket package. A code like this always connects:
ws = websocket.WebSocketApp("wss://", on_message = on_message, on_error = on_error, on_close = on_close)
Yet the package doesn’t have any emit() built-in function and can only send strings like this ws.send(b'42["game/move", {"move": "ab", "auth": "1345567890", "game_id": 9024067, "player_id": 432818}]')

As using the websocket package looks more promising, I’ve created an interface for my bot based on it. I can use it to play a game and receive events from the real time API. But… I am regularly disconnected from the server. As a workaround, I recreate a connection every time I’m disconnected and then make sure everything is safe and synchronized between the bot and the server. But this is quite tricky and time consuming so I would rather keep the connection alive for hours.
The websocket connection is like this:

def on_message(ws, message):
     print("message", message)

def on_error(ws, error):
    print("error", error)

def on_close(ws):

def on_open(ws):

ws = websocket.WebSocketApp("wss://", on_message=on_message, on_error=on_error, on_close=on_close)
ws.on_open = on_open
start = time.time()
print('total time:', time.time() - start)

If I don’t define ping_interval, the connection will close after 30 seconds (exactly)
If I define a ping (whatever the value), the connection will close after 85 seconds (exactly)

Execution example:

--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: /xDWSAIfMMbKF2Bmfb/+ew==
Sec-WebSocket-Version: 13

--- response header ---
HTTP/1.1 101 Switching Protocols
Date: Sat, 10 Jun 2017 19:46:00 GMT
Connection: upgrade
Set-Cookie: __cfduid=d47c499ff361a5db3f34aaef0239c2b2a1497123960; expires=Sun, 10-Jun-18 19:46:00 GMT; path=/;; HttpOnly
Upgrade: websocket
Sec-WebSocket-Accept: GhGAlOSxViNTGqKbUMiR54GpPeQ=
Server: cloudflare-nginx
CF-RAY: 36ced5912a066ecf-NRT
message 0{"sid":"BwPpNI9rCnuxNtBcAH_B","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
message 40
message 42["active-bots",{"58441":{"username":"GnuGo","ranking":21,"ranking_blitz":20,"ranking_live":21,"icon-url":"","country":"un","pro":0,"ui_class":"bot provisional","id":58441,"ranking_correspondence":21},"58542":{"username":"Fuego","ranking":25,"ranking_blitz":25,"ranking_live":25,"icon-url":"","country":"un","pro":0,"ui_class":"bot provisional","id":58542,"ranking_correspondence":24},"130032":{"username":"Kugutsu","ranking":20,"ranking_blitz":17,"ranking_live":21,"icon-url":"","country":"_DOOP","pro":0,"ui_class":"bot provisional","id":130032,"ranking_correspondence":21},"165325":{"username":"Nicaso","ranking":30,"ranking_blitz":29,"ranking_live":30,"icon-url":"","country":"wf","pro":0,"ui_class":"bot provisional","id":165325,"ranking_correspondence":28},"419331":{"username":"RoyalLeela","ranking":34,"ranking_blitz":34,"ranking_live":34,"icon-url":"","country":"us","pro":0,"ui_class":"bot","id":419331,"ranking_correspondence":34},"434218":{"username":"DarkGo","ranking":33,"ranking_blitz":33,"ranking_live":33,"icon-url":"","country":"_Pirate","pro":0,"ui_class":"bot provisional","id":434218,"ranking_correspondence":31}}]
send: b'\x89\x80\x13\x9c,\xa3' <-- ping
send: b'\x89\x80rHHD'
send: b'\x89\x80keY6'
send: b'\x88\x82\x183W\xa5\x1b\xdb'
total time: 85.67800807952881

Is there a reason behind these 30/85 seconds timeout?
The first message I receive says "pingInterval":25000,"pingTimeout":60000. Maybe it’s just a coincidence but 25000 + 60000 = 85000 = 85s? Then it would mean the server is supposed to ping me but fails and thus closes the connection.


I have a similar problem:

Whatever I do, the connection only lasts about 30 seconds - even if messages are actively send.

Before that happens I receive:
send: b’\x89\x80?\xef\xbf!’
and then
send: b’\x88\x82\xa8\x1f\x9f\xb0\xab\xf7’

in my terminal.

Does anyone has an explanation by now for this?

If you use the websocket-client package, ping_interval doesn’t work with the Socket.IO protocol (?)
Socket.IO is built on top of the Engine.IO protocol where the first number of a “message” refers to the packet type id (GitHub - socketio/
The server sends a ping (‘2’) every “pingInterval” seconds and expects from the client a pong (‘3’) to keep the connection alive. If the client doesn’t pong back then the connection is closed after “pingTimeout” seconds.

Example of a simple solution:

def on_message(ws, message):
    if(message[0] == '2'):