Skip to content

Forwarding messages to websocket #1844

Closed Answered by dev-perry
dev-perry asked this question in Q&A
Discussion options

You must be logged in to vote

I found a StackOverflow article that showed that subscriber.start() is actually NOT blocking, therefore I needed to use a blocking loop to keep the socket connection open. Proper approach:

from faststream.rabbit.fastapi import RabbitRouter, RabbitMessage
from faststream.rabbit import RabbitQueue, RabbitBroker
from fastapi import WebSocket, Depends
from typing_extensions import Annotated
from fastapi.websockets import WebSocketDisconnect


broker_router = RabbitRouter("amqp://guest:guest@localhost:5672/", max_consumers=1)

def broker():
    return broker_router.broker


@broker_router.websocket("/ws/collection/{cid}/{eid}")
async def stream_collection(
    websocket: WebSocket,
    cid: str,…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Lancetnik
Comment options

Answer selected by dev-perry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants