Documentation
¶
Overview ¶
Package ywebsocket implements the server side of the Yjs y-websocket protocol as a RELAY. We don't decode CRDT updates server-side — the clients sort it out among themselves via Yjs's update + sync-step algebra. Our job :
- track rooms (one per project document)
- when a client joins, broadcast their incoming binary frames to every OTHER client in the same room
- when a client emits a "sync-step-1" handshake, the existing clients reply with their current state ; we don't intercept
- on the last client leaving, the room is GC'd (state is gone — V0.1 has no persistence ; V0.2 will snapshot to weft-block)
Why a relay (and not a server-side CRDT) :
- the y-protocols spec is binary + versioned ; clients evolve and a relay is forward-compatible with new protocol versions for free
- server-side state needs a Go Yjs implementation, which is a several-thousand-line project on its own
- relay-only is the deployment mode the upstream y-websocket server uses too ; persistence is bolted on by serialising the last update from any one client to a backing store
Concurrency : one goroutine per connection reads from the socket and pushes onto the hub's broadcast channel ; the hub fan-out goroutine pushes to every peer's send channel. Slow peers get dropped (we never block the hub on one straggler).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConnID ¶
type ConnID string
ConnID is the opaque connection identifier the caller passes in. Typically the remote-addr ; only used for log lines + tie-breaking in awareness.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub multiplexes connections by RoomID. One Hub per server instance.
func NewHub ¶
func NewHub() *Hub
NewHub builds an empty Hub. Long-lived ; no Shutdown method — the process exit takes everything with it. Per-room cleanup happens on the last-client-leaves path.
func (*Hub) Join ¶
func (h *Hub) Join(roomID string, conn ConnID) *Membership
Join inserts the connection into the named room (creating the room if needed) and returns a handle the caller uses to read + write + leave. The handle's Recv channel delivers every binary frame from peers ; the caller writes their outbound frames via Send. Leave unregisters.
type Membership ¶
type Membership struct {
// contains filtered or unexported fields
}
Membership is one client's view into a Room. Returned by Hub.Join.
func (*Membership) Leave ¶
func (m *Membership) Leave()
Leave removes the membership from its room and closes Recv. Idempotent ; second call is a no-op.
func (*Membership) LeaveOnContextDone ¶
func (m *Membership) LeaveOnContextDone(ctx context.Context)
LeaveOnContextDone wires the Membership to ctx — when ctx cancels, the membership leaves automatically. Saves the caller a goroutine.
func (*Membership) Recv ¶
func (m *Membership) Recv() <-chan []byte
Recv returns the channel of peer frames. Closed when the Membership leaves.
func (*Membership) Send ¶
func (m *Membership) Send(payload []byte)
Send broadcasts the binary frame to the rest of the room. Safe concurrent with Recv on the same Membership.