Building WebSocket Servers and Clients with asyncio – Python Lore

Building WebSocket Servers and Clients with asyncio – Python Lore

WebSockets provide a way to establish a persistent, full-duplex communication channel between a client and a server over the web. Unlike the traditional HTTP request/response model, which is unidirectional, WebSockets allow for bidirectional communication, enabling servers to send data to clients without the need for the client to initiate a request. This makes WebSockets perfect for real-time applications such as live chats, gaming, and financial trading platforms.

At the core of the WebSocket protocol is the WebSocket API, which is supported in most state-of-the-art browsers and allows for easy integration into web applications. The protocol is defined in RFC 6455 and uses ws or wss (WebSocket Secure) as its URI scheme. The handshake process begins with an HTTP request that includes an Upgrade header, signaling the server that the client wishes to establish a WebSocket connection. If the server supports WebSockets, it responds with an HTTP 101 status code (Switching Protocols) and completes the handshake, upgrading the connection from HTTP to WebSockets.

One of the main advantages of using WebSockets is that it minimizes overhead and latency by allowing messages to be sent and received without the need to open and close connections for each exchange. This is facilitated by the use of “frames” to encapsulate data, which can be sent back and forth over the same connection once established.

To illustrate how a WebSocket connection is established, here is a simple Python example using the built-in websockets library:

import asyncio
import websockets

async def echo(websocket, path):
    async for message in websocket:
        await websocket.send(message)

start_server = websockets.serve(echo, "localhost", 8765)

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

In this example, we define an asynchronous function echo that simply sends back any message it receives. We then start a WebSocket server that listens on localhost at port 8765 and runs indefinitely. Any client that connects to this server will be able to engage in a two-way communication using WebSockets.

With this foundational understanding of WebSockets, we can delve into how to implement WebSocket servers and clients using Python’s asyncio library in the following sections.

II. Implementing a WebSocket Server with asyncio

Implementing a WebSocket server with asyncio involves a few essential steps. Firstly, you need to have the websockets library installed in your Python environment. You can install it using pip:

pip install websockets

Once you have the library installed, creating your server is relatively straightforward. The following example demonstrates how to build a simple echo server, which is a server that sends back to the client whatever it receives.

import asyncio
import websockets

async def echo(websocket, path):
    async for message in websocket:
        print(f"Received message: {message}")
        await websocket.send(f"Echo: {message}")

async def main():
    async with websockets.serve(echo, "localhost", 8765):
        await asyncio.Future()  # run forever

if __name__ == "__main__":
    asyncio.run(main())

In this example, we define an echo coroutine that takes a websocket and a path as arguments. The async for loop listens for incoming messages from the client, and then sends a response back by prefixing the received message with “Echo:”. The main coroutine sets up the server to listen on localhost at port 8765 and runs indefinitely.

Here is another example, demonstrating how to handle multiple clients concurrently:

import asyncio
import websockets

connected = set()

async def echo(websocket, path):
    connected.add(websocket)
    try:
        async for message in websocket:
            print(f"Received message: {message}")
            for conn in connected:
                if conn != websocket:
                    await conn.send(f"New message: {message}")
    finally:
        connected.remove(websocket)

async def main():
    async with websockets.serve(echo, "localhost", 8765):
        await asyncio.Future()  # run forever

if __name__ == "__main__":
    asyncio.run(main())

In this version of the server, we maintain a set of connected clients. Whenever a message is received from one client, it’s broadcasted to all other connected clients. This kind of pattern is common in chat applications, where all participants need to see messages from each other. The try/finally block ensures that the client is removed from the connected set when the connection is closed, which is important for cleaning up resources and avoiding potential memory leaks.

With these examples, you can see how easy it is to set up a basic WebSocket server using asyncio and the websockets library. These servers can serve as the starting point for more complex applications that require real-time communication between clients and servers.

III. Creating a WebSocket Client with asyncio

Now, let’s turn our attention to creating a WebSocket client with asyncio. Just as with the server, you will need the websockets library installed in your Python environment to get started. With that in place, we can write an asyncio program that connects to a WebSocket server and sends and receives messages.

import asyncio
import websockets

async def hello():
    uri = "ws://localhost:8765"
    async with websockets.connect(uri) as websocket:
        await websocket.send("Hello, world!")
        response = await websocket.recv()
        print(f"Received: {response}")

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

In this example, we define an async function named hello that connects to a WebSocket server at ws://localhost:8765. We then send a message “Hello, world!” to the server and wait for a response using the recv() method. Once we receive a response, we print it out.

To handle incoming messages from the server continuously, we can modify our client to use an async for loop:

import asyncio
import websockets

async def listen():
    uri = "ws://localhost:8765"
    async with websockets.connect(uri) as websocket:
        async for message in websocket:
            print(f"Received: {message}")

asyncio.get_event_loop().run_until_complete(listen())

This client will consistently listen for new messages from the server and print them as they come in. It’s important to note that in a real-world application, you would need to handle exceptions and reconnect logic in case of connection errors or drops.

For more advanced usage, you might want to send data periodically or based on certain conditions:

import asyncio
import websockets
import time

async def send_periodically(uri):
    async with websockets.connect(uri) as websocket:
        while True:
            await websocket.send(f"Current time: {time.time()}")
            await asyncio.sleep(1)  # Send message every second

uri = "ws://localhost:8765"
asyncio.get_event_loop().run_until_complete(send_periodically(uri))

In this code snippet, our client connects to the server and sends the current time every second. That is done by using a while True loop and asyncio.sleep() for the delay.

By using Python’s asyncio library and the websockets library, you can create powerful WebSocket clients capable of handling real-time communication with a server. Whether it’s for a chat application, a live data feed, or any other use case where quick and efficient bi-directional communication is required, asyncio and WebSockets are a robust solution.

IV. Advanced Features and Best Practices

As we delve into more advanced features and best practices for building WebSocket servers and clients with asyncio, it’s important to consider various aspects such as connection management, error handling, and security.

One common practice in WebSocket applications is to implement a ping/pong mechanism to keep the connection alive and detect any potential connection issues. This can be easily achieved in asyncio using the ping() and pong() methods provided by the websockets library:

import asyncio
import websockets

async def serve(websocket, path):
    while True:
        try:
            await websocket.ping()
            await asyncio.sleep(10)  # Send a ping every 10 seconds
        except websockets.exceptions.ConnectionClosed:
            break

start_server = websockets.serve(serve, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

In this example, the server sends a ping to the client every 10 seconds, which helps ensure that the connection is still alive. If the connection is closed, an exception will be raised, and the loop will break, effectively ending the coroutine for that client.

When it comes to error handling, it’s important to catch exceptions that may occur during the WebSocket communication. This includes handling unexpected disconnections, protocol errors, or other network-related issues:

import asyncio
import websockets

async def serve(websocket, path):
    try:
        async for message in websocket:
            # Process incoming messages
            pass
    except websockets.exceptions.ConnectionClosedError as e:
        print(f"Connection closed with error: {e}")
    except Exception as e:
        print(f"Unhandled exception: {e}")

start_server = websockets.serve(serve, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

This error handling ensures that your server can recover gracefully from errors and continue to serve other clients without interruption.

Security is another important aspect to ponder when building WebSocket applications. Using WebSocket Secure (wss) instead of WebSocket (ws) ensures that the data exchanged between the client and server is encrypted. That’s especially important when dealing with sensitive information. Configuring SSL for your asyncio WebSocket server can be done as follows:

import asyncio
import websockets
import ssl

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile='path/to/cert.pem', keyfile='path/to/key.pem')

start_server = websockets.serve(serve, "localhost", 8765, ssl=ssl_context)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

In this example, we create an SSL context with the required certificate and key files, then pass this context when starting the server. Clients connecting to this server must use the wss URI scheme to establish a secure connection.

Finally, it’s a best practice to organize your WebSocket code into modular components, especially as your application grows in complexity. For example, you may want to separate your message handling logic into different functions or classes:

import asyncio
import websockets

class WebSocketServer:
    def __init__(self):
        self.connected_clients = set()

    async def handler(self, websocket, path):
        self.connected_clients.add(websocket)
        try:
            async for message in websocket:
                await self.process_message(message)
        finally:
            self.connected_clients.remove(websocket)

    async def process_message(self, message):
        # Custom logic for processing incoming messages
        pass

server = WebSocketServer()
start_server = websockets.serve(server.handler, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

By following these advanced features and best practices, you can ensure that your WebSocket servers and clients built with asyncio are robust, secure, and maintainable.

Source: https://www.pythonlore.com/building-websocket-servers-and-clients-with-asyncio/


You might also like this video