wsroom

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 8 Imported by: 0

README

wsroom

wsroom is a lightweight WebSocket room hub for Go applications. It manages room membership, targeted sends, broadcasts, reconnect grace periods, and hook-based lifecycle/message handling.

Install

go get github.com/a-sam-randall/wsroom@latest

Quick Start

package main

import (
	"context"
	"log"
	"net/http"
	"time"

	"github.com/a-sam-randall/wsroom"
)

type hooks struct{}

func (h *hooks) OnJoin(ctx context.Context, jc wsroom.JoinContext, client wsroom.Client) error {
	return nil
}

func (h *hooks) OnMessage(ctx context.Context, room wsroom.RoomID, p wsroom.ParticipantID, msg wsroom.Envelope) error {
	return nil
}

func (h *hooks) OnLeave(ctx context.Context, room wsroom.RoomID, p wsroom.ParticipantID, reason error) {}

func main() {
	hub := wsroom.NewHub(&hooks{}, wsroom.ReconnectPolicy{
		GracePeriod:      30 * time.Second,
		MaxQueuedPerConn: 64,
	})

	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		err := hub.HandleWS(w, r, wsroom.RoomID("game-1"), wsroom.Identity{}, func(ctx context.Context, room wsroom.RoomID, identity wsroom.Identity) (wsroom.ParticipantID, error) {
			return wsroom.ParticipantID("player-1"), nil
		})
		if err != nil {
			log.Printf("ws error: %v", err)
		}
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

Versioning

This module follows semantic versioning. Starting from v1.0.0, breaking API changes require a new major version.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRoomNotFound         = errors.New("wsroom: room not found")
	ErrParticipantNotFound  = errors.New("wsroom: participant not found")
	ErrParticipantOffline   = errors.New("wsroom: participant offline")
	ErrResolverRejected     = errors.New("wsroom: resolver rejected participant")
	ErrMissingParticipantID = errors.New("wsroom: empty participant id")
)

Functions

func NewHandler

func NewHandler(hub Hub, room RoomID, identity Identity, resolveParticipant ResolveParticipantFn) http.Handler

Types

type Client

type Client interface {
	Send(ctx context.Context, msg Envelope) error
	Close(code websocket.StatusCode, reason string) error
}

type Envelope

type Envelope struct {
	Type    string          `json:"type"`
	Payload json.RawMessage `json:"payload"`
}

type Handler

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

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Hooks

type Hooks interface {
	OnJoin(ctx context.Context, jc JoinContext, client Client) error
	OnMessage(ctx context.Context, room RoomID, p ParticipantID, msg Envelope) error
	OnLeave(ctx context.Context, room RoomID, p ParticipantID, reason error)
}

type Hub

type Hub interface {
	HandleWS(w http.ResponseWriter, r *http.Request, room RoomID, identity Identity, resolveParticipant ResolveParticipantFn) error
	SendTo(room RoomID, p ParticipantID, msg Envelope) error
	Broadcast(room RoomID, msg Envelope, exclude ...ParticipantID) error
	CloseRoom(room RoomID) error
}

func NewHub

func NewHub(hooks Hooks, policy ReconnectPolicy) Hub

type Identity

type Identity struct {
	UserID    string
	SessionID string
}

type JoinContext

type JoinContext struct {
	Room        RoomID
	Participant ParticipantID
	Identity    Identity
	Reconnect   bool
}

type ParticipantID

type ParticipantID string

type ReconnectPolicy

type ReconnectPolicy struct {
	GracePeriod      time.Duration
	MaxQueuedPerConn int
}

type ResolveParticipantFn

type ResolveParticipantFn func(ctx context.Context, room RoomID, identity Identity) (ParticipantID, error)

type RoomID

type RoomID string

Jump to

Keyboard shortcuts

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