Day 38: Web Socket in Python

Day 38: Web Socket in Python

Real-Time Collaboration Made Easy: WebSocket Integration in Python

ยท

2 min read

Welcome to the thirty-eighth installment of our Python Learning Series! Today, we delve into the fascinating realm of websockets in Python.

Websockets are a powerful communication protocol that allows real-time, full-duplex communication between a client (such as a web browser) and a server. Unlike traditional HTTP requests, which are stateless and require a new connection for each request, WebSockets enable persistent connections, enabling efficient data exchange for applications like chat systems, online gaming, and live data streaming.

Setup Websocket in FastAPI

First, make sure you have the websockets library installed. You can install it using pip:

pip install websockets

Now, let's create a simple WebSocket server:

import asyncio
import websockets

async def handle_websocket(websocket, path):
    # Logic to handle websocket messages
    async for message in websocket:
        # Process incoming message
        print(f"Received message: {message}")
        # Echo the message back to the client
        await websocket.send(message)

async def start_server():
    server = await websockets.serve(handle_websocket, 'localhost', 8765)
    print("Websocket server started...")
    await server.wait_closed()

asyncio.run(start_server())

Creating a WebSocket Client

To test our websocket server, let's create a simple websocket client:

import asyncio
import websockets

async def send_message():
    async with websockets.connect('ws://localhost:8765') as websocket:
        message = input("Enter a message: ")
        await websocket.send(message)
        response = await websocket.recv()
        print(f"Received response: {response}")

asyncio.run(send_message())

In this client code, we establish a WebSocket connection to our server running on localhost port 8765. We send a message entered by the user and print the response received from the server.

Conclusion

WebSockets open up exciting possibilities for building interactive and real-time web applications in Python. Whether you're creating a chat application, a live data dashboard, or a multiplayer game, understanding how to use websockets can greatly enhance your development capabilities.

Thank you๐Ÿ’•๐Ÿ’•

ย