fabric ·nustd.ws_server

Serve websocket clients straight from your program.

Run a websocket server from your Nu tree. Every client that connects gets its own arm of the program, and you decide what travels over the wire.

Powered byuvicornFastAPI

See it.

The server keeps track of who is connected. One arm of your program runs for each client, and ends when they go.

The lifetime of an arm is the lifetime of a socket.

A connection opens and an arm of your tree starts. It closes and the arm ends. That holds in both directions, so there is no reaping pass and no orphan to find later.

Nothing about the messages is fixed for you. The browser UI is this fabric with a protocol stacked on top, and yours stacks the same way.

server.py
python · 13 locpy
import asyncio
import nu
import nustd
# the arm one connection gets, handed its own transport
arm = nustd.ws_server.SessionFor(body=handle_client)
# boot the host, then run that arm once per live connection
app = nu.With(
nustd.ws_server.listen(session_cls=EchoSession, port=8765),
body=nustd.ws_server.sessions_fold(arm),
)
asyncio.run(nu.arun(app))

What you can do with it.

A real websocket server, written as part of the program.

any protocol

Speak whatever you want.

The fabric handles the sockets and hands each arm its connection. What you send over it is yours: JSON, msgpack, a binary format you invented.

per connection

One arm per client.

Each live connection runs its own arm of the tree, with its own state. Clients cannot see each other unless a fabric you bound lets them.

bracketed

Close is not your problem.

Sockets open and close inside the bracket, and the arm follows. No lifecycle callbacks, no cleanup you have to keep in sync with teardown.

Try Nu.

One command gets you the wheel with every fabric. Then follow the movies tutorial to build a real app in an afternoon.

01 Install

pip install "nustack-py[all]"

02 Run the demo

nu demo movies

03 Build your app

Browse examples

Like what you see?

The project is young. Star it, join the room, watch what we ship next.