ywebsocket

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

type Room

type Room struct {
	// contains filtered or unexported fields
}

Room is one collaborative document. All members see the same broadcast feed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL