actioncable

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package actioncable provides a server-side implementation of the Rails Action Cable protocol (https://docs.anycable.io/misc/action_cable_protocol)

Index

Constants

View Source
const (
	ActionCableV1JSONSubprotocol    = "actioncable-v1-json"
	ActionCableV1MsgpackSubprotocol = "actioncable-v1-msgpack"
)

Variables

This section is empty.

Functions

func SupportedSubprotocols added in v0.5.0

func SupportedSubprotocols() []string

Types

type Cancellers

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

Cancellers associates IDs with context cancellation functions so that those functions can be run by ID.

func NewCancellers

func NewCancellers() *Cancellers

NewCancellers creates a new instance of Cancellers.

func (*Cancellers) Add

func (c *Cancellers) Add(id string, canceller context.CancelFunc)

Add records an association between the ID and the context cancellation function.

func (*Cancellers) Cancel

func (c *Cancellers) Cancel(id string)

Cancel runs and then forgets all context cancellation functions associated with the ID.

type Channel

type Channel interface {
	// Subscribe handles an Action Cable subscribe command from the client with the provided
	// [Subscription].
	Subscribe(ctx context.Context, sub *Subscription) error
	// Perform handles an Action Cable action command from the client.
	Perform(data string) error
}

Channel represents a server-side Action Cable channel.

type ChannelDispatcher added in v0.5.0

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

ChannelDispatcher manages channels, channel subscriptions, and channel actions.

func NewChannelDispatcher added in v0.5.0

func NewChannelDispatcher(
	factories map[string]ChannelFactory, channels map[string]Channel, checkers ...IdentifierChecker,
) *ChannelDispatcher

func (*ChannelDispatcher) HandleAction added in v0.5.0

func (d *ChannelDispatcher) HandleAction(ctx context.Context, identifier, data string) error

HandleAction dispatches Action Cable action messages to the appropriate channels.

func (*ChannelDispatcher) HandleSubscription added in v0.5.0

func (d *ChannelDispatcher) HandleSubscription(ctx context.Context, sub *Subscription) error

HandleSubscription associates the subscription to its corresponding channel and calls the channel's Subscribe method, first instantiating the channel if necessary.

type ChannelFactory

type ChannelFactory func(identifier string) (Channel, error)

ChannelFactory creates a Channel from an Action Cable subscription identifier.

type Conn

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

Conn represents a server-side Action Cable connection.

func Upgrade

func Upgrade(wsc *websocket.Conn, handler Handler, opts ...ConnOption) (conn *Conn, err error)

Upgrade upgrades the WebSocket connection to an Action Cable connection. The WebSocket connection should not be written to or read from if the upgrade completes successfully.

func (*Conn) Close

func (c *Conn) Close(err error) error

Close cancels all subscriptions, sends an Action Cable disconnect message and WebSocket close control message (which can be interrupted by canceling the provided context), and closes the WebSocket connection. The Conn should not be used after being closed.

func (*Conn) Serve

func (c *Conn) Serve(ctx context.Context) (err error)

Serve processes all WebSocket ping/pong messages and Action Cable messages.

type ConnOption

type ConnOption func(conn *Conn)

ConnOption modifies a Conn. For use with Upgrade.

func WithErrorSanitizer

func WithErrorSanitizer(f ErrorSanitizer) ConnOption

WithErrorSanitizer creates a ConnOption to set the Conn's error sanitizer, which processes errors to avoid leaking sensitive information in server logs.

type DataPayload added in v0.5.0

type DataPayload interface {
	string | []byte
}

type ErrorSanitizer

type ErrorSanitizer func(err error) string

type Handler added in v0.5.0

type Handler interface {
	HandleSubscription(ctx context.Context, sub *Subscription) error
	HandleAction(ctx context.Context, identifier, data string) error
}

Handler handles Action Cable channel subscription and channel action messages.

type IdentifierChecker

type IdentifierChecker func(identifier string) error

IdentifierChecker returns an error if the provided identifier is invalid.

func WithCSRFTokenChecker

func WithCSRFTokenChecker(checker func(token string) error) IdentifierChecker

WithCSRFTokenChecker creates an IdentifierChecker which extracts a CSRF token from the Action Cable subscription identifier and checks it using the checker callback function.

type Signer added in v0.5.0

type Signer struct {
	Config SignerConfig
}

Signer creates and verifies subscription identifier names with an HMAC.

func NewSigner added in v0.5.0

func NewSigner(config SignerConfig) Signer

NewSigner creates a new instance of Signer.

func (Signer) Check added in v0.5.0

func (s Signer) Check(identifier string) error

Check parses the subscription identifier's name field and checks if it matches the HMAC in the subscription identiier's hash field. Note that these fields are not part of the Action Cable specification; rather, they're conventions inherited from the Rails implementation of integration between Turbo Streams and Action Cable.

func (Signer) Sign added in v0.5.0

func (s Signer) Sign(name string) (hash string)

Sign computes the base64-encoded string of the HMAC of the name.

type SignerConfig added in v0.5.0

type SignerConfig struct {
	HashKey []byte
}

func GetSignerConfig added in v0.5.0

func GetSignerConfig() (c SignerConfig, err error)

type Subscription

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

Subscription represents the server end of an Action Cable subscription.

func (*Subscription) Close

func (s *Subscription) Close()

Close releases any resources associated with the subscription. The Subscription should not be used after it's closed.

func (*Subscription) Identifier

func (s *Subscription) Identifier() string

Identifier returns the Action Cable subscription identifier.

func (*Subscription) SendBytes added in v0.5.0

func (s *Subscription) SendBytes(ctx context.Context, message []byte) error

SendBytes enqueues the string message for sending to the subscription's subscriber, blocking until the message is added to the queue.

func (*Subscription) SendText added in v0.5.0

func (s *Subscription) SendText(ctx context.Context, message string) error

SendText enqueues the string message for sending to the subscription's subscriber, blocking until the message is added to the queue.

Jump to

Keyboard shortcuts

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