If you're using (or interested) in the WebSocket support in Mangum, please read #225
Replies: 7 comments 24 replies
-
@koxudaxi you're the only WebSockets user that I have personally been able to confirm previously. Are you still using it, or have any feedback/thoughts on it becoming a separate project? |
Beta Was this translation helpful? Give feedback.
-
One use case we had was a Lambda accepting both SQS events and API Gateway WebSocket events, using FastAPI with the later. Our devs didn't see a public API for accessing the WebSocket connection when handling an SQS event and so chose to build their own implementation. What we'd need would be to look up the WebSocket by user_id. |
Beta Was this translation helpful? Give feedback.
-
I'm currently working on a big real time application using django-channels. Now it's time to deploy it and I would like to do it serverless. |
Beta Was this translation helpful? Give feedback.
-
I've recently thought of a hobby project that I want to use websockets for and wondered if mangum supported it, leading me here. I've had a look through the code base at v0.12.4 but haven't had chance to try it out yet. One thing that struck me is that I think the implementation may have been trying to do too much, particularly around the backend/storage stuff. I'm not familiar with many ASGI frameworks, but the fastapi websocket docs end with an example of storing connections in a simple list (obviously not suitable for serverless architecture, but that's not my point here), where as it looks like if I ran mangum 0.12.4 the connect and disconnect messages would never reach fastapi but instead be handled by which ever of the storage mechanisms I used? That feels like it should be the responsibility of my fastapi code and mangum should just pass the connect and disconnect messages through, am I missing something? |
Beta Was this translation helpful? Give feedback.
-
Latecomer to this discussion, but we are happy mangum users and are now investigating ASGI websocket support. Some background context: we have a Lambda-based app that uses FastAPI for REST APIs. We also have an API Gateway websocket API, that we manage "on the side", by inspecting the Lambda handler's incoming I am investigating what it would take to fit the websocket event traffic into the ASGI flow, so that our FastAPI routes could handle the websocket messages. The main motivation for this is to make the FastAPI code simple, facilitating hosting it in, e.g. class WsMangum(Mangum):
def __call__(self, event: LambdaEvent, context: LambdaContext) -> dict:
handler = self.infer(event, context)
logger.debug(f'{handler=}')
with ExitStack() as stack:
if self.lifespan in ("auto", "on"):
lifespan_cycle = LifespanCycle(self.app, self.lifespan)
stack.enter_context(lifespan_cycle)
if isinstance(handler, WsAPIGateway):
logger.debug('WebSocketCycle')
ws_storage = WebSocketStorage()
ws_cycle = WebSocketCycle(handler.request, handler.message_type, handler.connection_id, ws_storage)
resp = ws_cycle(self.app, handler.body)
else:
logger.debug('HTTPCycle')
http_cycle = HTTPCycle(handler.scope, handler.body)
resp = http_cycle(self.app)
logger.debug(f'{resp=}')
return handler(resp)
assert False, "unreachable" # pragma: no cover which then allows this @router.websocket_route('/')
async def websocket_endpoint(websocket: WebSocket):
if websocket.client_state is not WebSocketState.CONNECTED:
await websocket.accept()
data = await websocket.receive_text()
await websocket.send_text(f'Message text was: {data}') Even though I have not (yet) implemented a storage handler to reconstitute the My hope for an interface between API Gateway websocket and ASGI would be that, from ASGI's point of view, an incoming It seems a shame to go to all the work to persist the I realize that a real app can't rely on "luck". Assuming I can see the behavior I want, I'd then look at a cache in each Lambda function so it could know if a given |
Beta Was this translation helpful? Give feedback.
-
So, I am interested in this, but cannot find a succinct example how to use/configure it. The referenced page (https://mangum.io/websockets/) returns a 404....please advise. |
Beta Was this translation helpful? Give feedback.
-
I was able to get web sockets working with mangum/fastapi/custom code. You can check out results [here](https://qnamod.pragmavant.com) .. create event and share/open it in several browsers, then post some questions.
|
Beta Was this translation helpful? Give feedback.
-
Hello.
It has been about 6 months since WebSocket API support was re-introduced in version 0.12.0. Since that time I've not received any issues or seen mention elsewhere at all related to the WebSocket support, good or bad, which is surprising to me.
This lack of activity seems to indicate that either the design of the WebSocket event flow is flawed in some way (either in how it handles multiple events or the steps required to configure it), or it may just be an unpopular feature and most users of ASGI frameworks prefer traditional server solutions. I can only speculate at this point.
If anyone has thoughts on this topic then please share here. I want to get a better idea of how valuable this feature is to consider if it is worth continuing to maintain in this project, or potentially a niche enough use-case that it should be extracted into a different package.
Beta Was this translation helpful? Give feedback.
All reactions