together

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: AGPL-3.0 Imports: 7 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 together keeps Watch together sessions: people on different devices, at home or away, watching the same thing at the same moment.

Everything goes through this server. Whoever starts a session gets a short code to share; others join with it. When anyone plays, pauses, seeks or moves on to another title, the session's state changes and every member's player follows. Players wait for changes with Wait, which returns as soon as the state moves past the version they last saw, so a change reaches everyone within one round trip.

The state says where playback was at UpdatedAt and whether it's paused; players work out where it is now from the server's clock (ServerTime in every answer), and seek when they drift too far from it.

Index

Constants

View Source
const (
	ActionPlay  = "play"  // carry on playing from PositionMs
	ActionPause = "pause" // pause at PositionMs
	ActionSeek  = "seek"  // jump to PositionMs, keeping play or pause
	ActionItem  = "item"  // switch everyone to ItemID, from PositionMs
	ActionJoin  = "join"  // someone joined
	ActionLeave = "leave" // someone left
	ActionStart = "start" // the session started
)

Things people do in a session.

View Source
const (

	// MaxWait is the longest Wait holds a request.
	MaxWait = 25 * time.Second
)

Variables

View Source
var (
	ErrNoSession = errors.New("that Watch together code doesn't work; it may have ended")
	ErrNotMember = errors.New("join the session first")
	ErrFull      = errors.New("this Watch together session is full")
	ErrTooMany   = errors.New("too many Watch together sessions right now")
	ErrAction    = errors.New("unknown action")
	ErrExists    = errors.New("that session is still going; join it")
)

Functions

func NormalizeCode

func NormalizeCode(code string) string

NormalizeCode takes a code as someone typed it.

Types

type Hub

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

Hub holds every session. The zero value isn't usable; call New.

func New

func New() *Hub

func (*Hub) Act

func (h *Hub) Act(code string, p Person, action string, positionMs, itemID int64) (State, error)

Act is a member playing, pausing, seeking or switching titles.

func (*Hub) Close

func (h *Hub) Close()

Close is the server shutting down: waits return at once, so players hear about the restart and come back to their session afterwards (see Start).

func (*Hub) End

func (h *Hub) End(code string, p Person) error

End stops a session for everyone.

func (*Hub) Join

func (h *Hub) Join(code string, p Person) (State, error)

Join adds p's player to a session, or welcomes it back.

func (*Hub) Leave

func (h *Hub) Leave(code string, p Person) error

Leave takes p's player out of a session.

func (*Hub) Peek

func (h *Hub) Peek(code string) (State, error)

Peek is a session's state for someone deciding whether to join.

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run drops members who stopped asking and sessions nobody came back to, until ctx ends.

func (*Hub) Start

func (h *Hub) Start(p Person, itemID, positionMs int64, paused bool, resume string) (State, error)

Start makes a session around what p is playing, with p as its host.

Sessions live in memory, so a server restart loses them. A member whose session is gone starts it again under the same code (resume), with what its own player has, and the others join it again as they come back.

func (*Hub) Wait

func (h *Hub) Wait(ctx context.Context, code string, p Person, since int64, wait time.Duration) (State, error)

Wait returns the session's state once its version passes since, or when the wait runs out. Asking counts as being there.

type Last

type Last struct {
	Action   string `json:"action"`
	By       string `json:"by"`
	ClientID string `json:"clientId"`
	At       int64  `json:"at"` // milliseconds
}

Last is the latest thing someone did, for players to show ("Sam paused").

type Member

type Member struct {
	ClientID string `json:"clientId"`
	UserID   int64  `json:"userId"`
	Name     string `json:"name"`               // the person
	Device   string `json:"device,omitempty"`   // such as "Living room TV"
	Platform string `json:"platform,omitempty"` // web, ios, android, tvos…
	Host     bool   `json:"host,omitempty"`     // started the session
	JoinedAt int64  `json:"joinedAt"`
	// contains filtered or unexported fields
}

Member is someone's player in a session.

type Person

type Person struct {
	UserID   int64
	Name     string
	ClientID string
	Device   string
	Platform string
}

Person is who's asking.

type State

type State struct {
	Code       string   `json:"code"`
	Version    int64    `json:"version"`
	ItemID     int64    `json:"itemId"`
	PositionMs int64    `json:"positionMs"` // where playback was at UpdatedAt
	Paused     bool     `json:"paused"`
	UpdatedAt  int64    `json:"updatedAt"`  // milliseconds, server clock
	ServerTime int64    `json:"serverTime"` // milliseconds, when this was sent
	Members    []Member `json:"members"`
	Last       *Last    `json:"last,omitempty"`
	// Ended means the session is over; players stop following it.
	Ended bool `json:"ended,omitempty"`
}

State is a session as members see it.

Jump to

Keyboard shortcuts

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