permitio / fastapi_websocket_pubsub

A fast and durable Pub/Sub channel over Websockets. FastAPI + WebSockets + PubSub == ⚡ 💪 ❤️
https://permit.io
MIT License
481 stars 37 forks source link

I can't change the channel always trigger the default "EventNotifier" #81

Open jorgeav527 opened 1 month ago

jorgeav527 commented 1 month ago
from fastapi_websocket_pubsub import PubSubEndpoint
from pydantic import BaseModel
from datetime import datetime

class ActivityLog(BaseModel):
    channel: str
    action: str
    user_id: str
    created: datetime

endpoint = PubSubEndpoint(
    broadcaster="redis://user:password@host:port"
)

@shared_router.websocket("/pubsub")
async def websocket_rpc_endpoint(websocket: WebSocket):
    await endpoint.main_loop(websocket)

@shared_router.post("/publish")
async def post_to_redis_pubsub(simple_message: ActivityLog):
    await endpoint.publish(
        topics=[simple_message.channel], data={"user_id": simple_message.user_id, "created": simple_message.created}
    )
    return {"message": "Event published"}

when I run the endpoint with swagger or curl

curl -X 'POST' \
  'http://localhost:7777/shared/publish' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "channel": "string",
  "action": "string",
  "user_id": "string",
  "created": "2024-08-01T18:26:07.523Z"
}'

the message is successfully sent but the channel do not change this I could see on the redis insight

Timestamp: 13:26:17 01 Aug 2024
Channel: "EventNotifier"
Message: {"notifier_id":"e07a909adf6d44dea3e9ab1df25cf1cd","topics":["string"],"data":{"user_id":"string","created":"2024-08-01T18:26:07.523000Z","channel":"string"}}
orweis commented 1 month ago

Hi, not sure what you're trying to do. The story here is missing clients/subscribers that would receive the event. Note the broadcaster with Redis is only meant to propagate the message to more PubSub servers (and through them to their clients) - it is usually not needed if you're not scaling out.