Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python websockets send to client and keep connection alive

I'm using python websockets: https://websockets.readthedocs.io/

They have a simple client/server example, where the server echoes the client's input back once. The code looks like this:

Client side:

# WS client example

import asyncio
import websockets

async def hello():
    async with websockets.connect(
            'ws://localhost:8765') as websocket:
        name = input("What's your name? ")

        await websocket.send(name)
        print(f"> {name}")

        greeting = await websocket.recv()
        print(f"< {greeting}")

asyncio.get_event_loop().run_until_complete(hello())

Server side:

# WS server example

import asyncio
import websockets

async def hello(websocket, path):
    name = await websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    await websocket.send(greeting)
    print(f"> {greeting}")

start_server = websockets.serve(hello, 'localhost', 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I want to adapt just the server side only, so that it does the following upon a socket connection:

  1. Send an acknowledgement message to the client. e.g. Hello Client! Please wait for your data.
  2. Keep the connection alive.
  3. Process some data that takes some time.
  4. After the data has completed processing, notify the client on the existing websocket connection. e.g. Your data is here!

The python websockets documentation doesn't have a code sample which does this.

like image 690
Anton Avatar asked Nov 16 '18 03:11

Anton


People also ask

Does WebSocket use keep alive?

Keepalive in websockets To avoid these problems, websockets runs a keepalive and heartbeat mechanism based on WebSocket Ping and Pong frames, which are designed for this purpose.

Can WebSockets send and receive at the same time?

The key word in that definition is two-way: with WebSocket, both the client and the server can trigger communication with one another, and both can send messages, at the same time.

Can a WebSocket server send message to client?

With WebSockets: the server can send a message to the client without the client explicitly requesting something. the client and the server can talk to each other simultaneously. very little data overhead needs to be exchanged to send messages.


2 Answers

To keep the connection open do not terminate the handler after processing the first message. For example, you can have an endless-loop that will keep processing the incoming messages until the connection is closed by the client:

async def hello(websocket, path):
    while True:
        try:
            name = await websocket.recv()
        except websockets.ConnectionClosed:
            print(f"Terminated")
            break

        print(f"< {name}")
        greeting = f"Hello {name}!"

        await websocket.send(greeting)
        print(f"> {greeting}")

In the async fun you can then await any long running operation as suggested here.

You will however need to adapt both server and client side in the similar way. Your client also terminates after receiving the first message.

like image 181
Petr Gotthard Avatar answered Oct 09 '22 20:10

Petr Gotthard


Presumably your function that processes the data is blocking, otherwise you'd simply await it inside the coroutine. The straightforward approach is to use run_in_executor to run it in another thread, and await it in your handler coroutine:

async def hello(websocket, path):
    loop = asyncio.get_event_loop()
    await websocket.send("Hello Client! Please wait for your data.")
    data = await loop.run_in_executor(None, get_data)
    await websocket.send("Your data is here!")
    await websocket.send(data)

def get_data():
    # something that takes a long time to calculate
    x = 19134702400093278081449423917**300000 % 256
    return bytes([x])
like image 24
user4815162342 Avatar answered Oct 09 '22 19:10

user4815162342