Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socketio.Client() keep connection, but socketio.AsyncClient() does not #324

Closed
yibaoren opened this issue Jul 31, 2019 · 16 comments
Closed
Assignees
Labels

Comments

@yibaoren
Copy link

yibaoren commented Jul 31, 2019

I want to maintain a long connection, since I subscribe some topics.
When I use socketio.Client() as a client, it do as what I expect.
However, when I use socketio.AsyncClient(), it disconnect after a while maybe 1 min.
I want a asynchorous client, can anyone help me?

following are the codes
def run_sync_socketio(url=URL):

sio = socketio.Client()

@sio.event
def connect():
    print('connection established')

@sio.event
def disconnect():
    print('disconnected from server')

@sio.on('rs.depth')
def depth(data):
    print('depth received with ', data)

@sio.on('rs.deal')
def deal(data):
    print('deal received with ', data)

@sio.on('push.symbol')
def sub(data):
    print('sub received with ', data)

sio.connect(url, transports=['websocket'])
sio.emit('sub.symbol', {'symbol': 'VDS_USDT'}, callback=callbk)
sio.wait()  # 不发心跳,可以保持连接接收消息

async def run_socketio(url=URL):

sio = socketio.AsyncClient()

@sio.event
def connect():
    print('connection established')

@sio.event
def disconnect():
    print('disconnected from server')

@sio.on('rs.depth')
def depth(data):
    print('depth received with ', data)

@sio.on('rs.deal')
def deal(data):
    print('deal received with ', data)

@sio.on('push.symbol')
def sub(data):
    print('sub received with ', data)

await sio.connect(url, transports=['websocket'])  # transports参数必须设置
await sio.emit('sub.symbol', {'symbol': 'VDS_USDT'}, callback=callbk)
# sio.start_background_task(background_task, sio)
await sio.wait()    # 为什么异步无法保持连接
@miguelgrinberg
Copy link
Owner

Can you get logs from the case that disconnects? Add logger=True, engineio_logger=True arguments to the AsyncClient() class constructor.

@yibaoren
Copy link
Author

yibaoren commented Aug 1, 2019

After adding logger=True, engineio_logger=True arguments to the AsyncClient(),I get the logs in the attachment.
Please tell me as soon as you find out the problem. @miguelgrinberg

test.log

@miguelgrinberg
Copy link
Owner

According to the log, the WebSocket connection was interrupted:

Read loop: WebSocket connection was closed, aborting

This makes the client attempt a reconnection, which in your case succeeded. I'm not sure why the disconnection occurs, this is reported by the websocket library so it's not being done by this package. Maybe you should see if you can get logs from the server that show more information.

@yibaoren
Copy link
Author

yibaoren commented Aug 2, 2019

The problem can be ascribed to the async websockets library (https://github.com/aaugustin/websockets). And I found the async websocket client in aiohttp library (https://github.com/aio-libs/aiohttp) works well. So, I hope your team can make a substitution. Maybe this is a solution.

@TarekAA
Copy link

TarekAA commented Aug 21, 2019

Same problem with socketio.AsyncClient(). client connects and maintain the connection for a short period of time, followed by disconnection then connection retry.
The connection retry succeeds and the loop of connection / disconnection continue for two or more iterations.
I tried socketio.Client() without the aforementioned problem.

@miguelgrinberg miguelgrinberg self-assigned this Nov 17, 2019
@miguelgrinberg
Copy link
Owner

I will see if replacing the websocket client with the one in aiohttp addresses this problem.

@miguelgrinberg
Copy link
Owner

@yibaoren @TarekAA Could you please install the master branch of python-engineio and retest this problem? The updates that I just committed to engineio master will use aiohttp's WebSocket client. Thanks1

@secretlyvogon
Copy link

Hi I am facing the same issue of AsyncClient being unable to hold the connection. I enabled logging to check what is going wrong and I see the following error after an emit.

Sending packet PING data None
PONG response has not been received, aborting
Unexpected error "WebSocket read returned None", aborting
Waiting for write loop task to end
Exiting ping task
Exiting write loop task
Waiting for ping loop task to end
Engine.IO connection dropped
Exiting read loop task
Connection failed, new attempt in 0.70 seconds

after that it successfully reconnects.

I am running 3 clients in parallel and mostly they all reconnect successfully, but if they can't they show the following error:

AttributeError: 'int' object has no attribute 'encode'

@miguelgrinberg
Copy link
Owner

@secretlyvogon there is an open issue for this: #417. Hopefully will have a solution in the next few days.

@secretlyvogon
Copy link

@miguelgrinberg as per your suggestion in #417 I installed the master branch and retested, but I am still getting the same error I mentioned above.

@miguelgrinberg
Copy link
Owner

@secretlyvogon did you install the master branch of this repo, or python-engineio? The fix is in the engineio repo, not here.

@secretlyvogon
Copy link

secretlyvogon commented Apr 13, 2020

I installed both from their respective master branches, and the error is still present. I will give you a little more context on what my code is doing and where exactly it is getting stuck.

On the server-side, I have defined an async coroutine, which communicates with the connected nodes. The clients receive the data, perform their functions, send an emit back to the server, and wait. The server collects data sent by each client. Once all the emits are collected, the server calls that coroutine again. Thus, forming a loop.

The first iteration of this loop executes properly, and the updates are collected as expected. However, right when the coroutine is called, the code seems to act oddly at the server end. By the time the coroutine emits, the clients are disconnected, and the following appears on the server-side log:

Client is gone, closing socket

I have already included the client side logs above.

@miguelgrinberg
Copy link
Owner

@secretlyvogon isn't this a different problem though? You indicated that you were getting this one:

AttributeError: 'int' object has no attribute 'encode'

@secretlyvogon
Copy link

You are right. I am sorry for getting it confused. I was earlier getting that specific error but after installing the fix it went away. However, I am still facing some difficulty with making AsyncClient retain a connection to the Server. Should I create a new issue with all the details I have included here?

@miguelgrinberg
Copy link
Owner

@secretlyvogon this is likely a blocking issue on your side. The "client is gone" error happens when the regular ping/pong packets are interrupted. This can be because your client blocks and fails to send a ping at the right interval, or because your server blocks and is unable to process the pings in a timely manner. If you are using asyncio only on the server, then I would suspect that side more. If you are also using asyncio on the clients, then the blockage can be in the clients or the server.

@secretlyvogon
Copy link

@miguelgrinberg Thank you for your suggestions. I was using async on both sides, so I tried with a regular client instead, and the results were the same. I feel like the issue is with the server itself. I am creating a new issue specific for my error as I feel any further discussion will not be relevant to this particular issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants