PHP Classes

File: apppython/routes/message_routes.py

Recommend this page to a friend!
  Classes of Rodrigo Faustino   Chat SSE   apppython/routes/message_routes.py   Download  
File: apppython/routes/message_routes.py
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Chat SSE
Exchange chat messages using server-sent events
Author: By
Last change:
Date: 6 months ago
Size: 887 bytes
 

Contents

Class file image Download
from fastapi import APIRouter from starlette.responses import StreamingResponse import json import asyncio from models.message import Message from database.database import insert_message, get_new_messages router = APIRouter() @router.post("/send_message/") async def send_message(message_body: Message): print(message_body) await insert_message(message_body.message,message_body.usuario) return {"message": "mensagem enviada."} async def event_generator(): last_id = 0 while True: new_messages = await get_new_messages(last_id) if new_messages: for msg in new_messages: yield f"data: {json.dumps(msg)}\n\n" last_id = msg['id'] await asyncio.sleep(1) @router.get("/events/", status_code=200) async def get_events(): return StreamingResponse(event_generator(), media_type="text/event-stream")