Skip to content

Commit c379c4d

Browse files
authored
Create a WSHandler per session (#25)
1 parent f57e4a7 commit c379c4d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/bokeh_fastapi/application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
251251
for route, ctx in self._applications.items():
252252
doc_handler = DocHandler(self, application_context=ctx)
253253
self.app.add_api_route(f"{route}", doc_handler.get, methods=["GET"])
254-
ws_handler = WSHandler(self, application_context=ctx)
254+
ws_handler = WSHandler.create_factory(self, application_context=ctx)
255255
route = route if route.endswith("/") else f"{route}/"
256-
self.app.add_websocket_route(f"{route}ws", ws_handler.ws_connect)
256+
self.app.add_websocket_route(f"{route}ws", ws_handler)
257257

258258
# Mount static file handlers
259259
for ext_name, ext_path in extension_dirs.items():

src/bokeh_fastapi/handler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def __init__(
140140
self.connection = None
141141
self._socket: WebSocket
142142

143+
@classmethod
144+
def create_factory(cls, application: BokehFastAPI, application_context: ApplicationContext):
145+
def create_handler(*args, **kwargs):
146+
inst = cls(application, application_context)
147+
return inst.ws_connect(*args, **kwargs)
148+
return create_handler
149+
143150
def check_origin(self, origin: str) -> bool:
144151
"""Implement a check_origin policy for Tornado to call.
145152

0 commit comments

Comments
 (0)