websocket and fastapi

lightsong發表於2024-12-04

This project demonstrates how to use socket.io with FastAPI and React

https://github.com/jrdeveloper124/socketio-app/tree/main

You can use WebSockets with FastAPI.

https://fastapi.tiangolo.com/advanced/websockets/

https://stackoverflow.com/questions/71827145/broadcasting-a-message-from-a-manager-to-clients-in-websocket-using-fastapi

How to send messages from server to client in a Python WebSockets server, AFTER initial handshake?

https://stackoverflow.com/questions/74988529/how-to-send-messages-from-server-to-client-in-a-python-websockets-server-after

import asyncio
import datetime
from typing import Iterator
import websockets
import random

websocket_connections = set()
sock_port = 8000
sock_url = 'localhost'
global_socket = lambda: None

async def register(websocket):
    print('register event received')
    websocket_connections.add(websocket) # Add this client's socket
    global_socket = websocket

async def poll_log():
    await asyncio.sleep(0.3) # Settle
    while True:
        await asyncio.sleep(0.3) # Slow things down
        
        # Send a dynamic message to the client after random delay
        r = random.randint(1, 10)
        if (r == 5): # Only send 10% of the time
            a_msg = "srv -> cli: " + str(random.randint(1,10000))
            print("sending msg: " + a_msg)
            websockets.broadcast(websocket_connections, a_msg) # Send to all connected clients
        
async def main():
    sock_server = websockets.serve(register, sock_url, sock_port)
    await asyncio.sleep(0.3) # Start up time
    async with sock_server: await poll_log()

if __name__ == "__main__":
    print("Websockets server starting up ...")
    asyncio.run(main())

websocket-example

https://github.com/ustropo/websocket-example/tree/main

Scaling WebSocket applications

https://unfoldai.com/fastapi-and-websockets/

import aioredis
from fastapi import FastAPI, WebSocket

app = FastAPI()

async def get_redis():
    redis = await aioredis.create_redis_pool("redis://localhost")
    return redis

@app.websocket("/ws/redis/{channel}")
async def websocket_endpoint(websocket: WebSocket, channel: str):
    await websocket.accept()
    redis = await get_redis()
    try:
        channel = await redis.subscribe(channel)
        async for message in channel[0].iter():
            await websocket.send_text(message.decode())
    finally:
        await redis.unsubscribe(channel)
        redis.close()
        await redis.wait_closed()

@app.post("/publish/{channel}")
async def publish(channel: str, message: str):
    redis = await get_redis()
    await redis.publish(channel, message)
    return {"message": "Published"}

useEffect

https://www.ruanyifeng.com/blog/2020/09/react-hooks-useeffect-tutorial.html

相關文章