Skip to content

Commit 5d7b9d6

Browse files
authored
Merge pull request #563 from callmephilip/incl-session-param-in-ws
Incl session param in ws
2 parents 1239708 + 69e29ce commit 5d7b9d6

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

fasthtml/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def _find_wsp(ws, data, hdrs, arg:str, p:Parameter):
239239
if arg.lower()=='htmx': return _get_htmx(hdrs)
240240
if arg.lower()=='app': return ws.scope['app']
241241
if arg.lower()=='send': return partial(_send_ws, ws)
242+
if 'session'.startswith(arg.lower()): return ws.scope.get('session', {})
242243
return None
243244
res = data.get(arg, None)
244245
if res is empty or res is None: res = hdrs.get(arg, None)

nbs/api/00_core.ipynb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@
796796
" if arg.lower()=='htmx': return _get_htmx(hdrs)\n",
797797
" if arg.lower()=='app': return ws.scope['app']\n",
798798
" if arg.lower()=='send': return partial(_send_ws, ws)\n",
799+
" if 'session'.startswith(arg.lower()): return ws.scope.get('session', {})\n",
799800
" return None\n",
800801
" res = data.get(arg, None)\n",
801802
" if res is empty or res is None: res = hdrs.get(arg, None)\n",
@@ -1228,7 +1229,7 @@
12281229
{
12291230
"data": {
12301231
"text/plain": [
1231-
"'a604e4a2-08e8-462d-aff9-15468891fe09'"
1232+
"'77486da1-c613-48be-80c4-9cae89eeec48'"
12321233
]
12331234
},
12341235
"execution_count": null,
@@ -1748,17 +1749,24 @@
17481749
"name": "stdout",
17491750
"output_type": "stream",
17501751
"text": [
1751-
"Message text was: Hi!, from client: Address(host='testclient', port=50000)\n"
1752+
"Message text was: Hi! with session bar, from client: Address(host='testclient', port=50000)\n"
17521753
]
17531754
}
17541755
],
17551756
"source": [
1757+
"@app.get(\"/setsess\")\n",
1758+
"def set_sess(session):\n",
1759+
" session['foo'] = 'bar'\n",
1760+
" return 'ok'\n",
1761+
"\n",
17561762
"@app.ws(\"/ws\")\n",
1757-
"def ws(self, msg:str, ws:WebSocket): return f\"Message text was: {msg}, from client: {ws.client}\"\n",
1763+
"def ws(self, msg:str, ws:WebSocket, session): return f\"Message text was: {msg} with session {session.get('foo')}, from client: {ws.client}\"\n",
1764+
"\n",
1765+
"cli.get('/setsess')\n",
17581766
"with cli.websocket_connect('/ws') as ws:\n",
17591767
" ws.send_text('{\"msg\":\"Hi!\"}')\n",
17601768
" data = ws.receive_text()\n",
1761-
"assert 'Message text was: Hi!' in data\n",
1769+
"assert 'Message text was: Hi! with session bar' in data\n",
17621770
"print(data)"
17631771
]
17641772
},
@@ -2415,13 +2423,13 @@
24152423
"name": "stdout",
24162424
"output_type": "stream",
24172425
"text": [
2418-
"Set to 2024-10-28 20:22:34.772989\n"
2426+
"Set to 2024-11-04 15:30:23.038930\n"
24192427
]
24202428
},
24212429
{
24222430
"data": {
24232431
"text/plain": [
2424-
"'Session time: 2024-10-28 20:22:34.772989'"
2432+
"'Session time: 2024-11-04 15:30:23.038930'"
24252433
]
24262434
},
24272435
"execution_count": null,
@@ -2786,7 +2794,7 @@
27862794
{
27872795
"data": {
27882796
"text/plain": [
2789-
"'Cookie was set at time 20:22:35.467691'"
2797+
"'Cookie was set at time 15:30:23.174646'"
27902798
]
27912799
},
27922800
"execution_count": null,

nbs/explains/websockets.ipynb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@
9393
"This is a fairly simple example and could be done just as easily with standard HTTP requests, but it illustrates the basic idea of how websockets work. Let's look at a more complex example next."
9494
]
9595
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"## Session data in Websockets\n",
101+
"\n",
102+
"Session data is shared between standard HTTP routes and Websockets. This means you can access, for example, logged in user ID inside websocket handler:\n",
103+
"\n",
104+
"```python\n",
105+
"from fasthtml.common import *\n",
106+
"\n",
107+
"app = FastHTML(exts='ws')\n",
108+
"rt = app.route\n",
109+
"\n",
110+
"@rt('/login')\n",
111+
"def get(session):\n",
112+
" session[\"person\"] = \"Bob\"\n",
113+
" return \"ok\"\n",
114+
"\n",
115+
"@app.ws('/ws')\n",
116+
"async def ws(msg:str, send, session):\n",
117+
" await send(Div(f'Hello {session.get(\"person\")}' + msg, id='notifications'))\n",
118+
"\n",
119+
"serve()\n",
120+
"```"
121+
]
122+
},
96123
{
97124
"cell_type": "markdown",
98125
"metadata": {},

0 commit comments

Comments
 (0)