package module
Version: v0.2.0 Latest Latest

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 9 Imported by: 1




A golang client library for Rails' ActionCable. I wrote this solely because I needed it and it's far from being a polished library.




This section is empty.


View Source
var ErrAlreadySubscribed = errors.New("channel already subscribed")

ErrAlreadySubscribed is returned when the client has already subscribed to a given channel.

View Source
var ErrNoChannelName = errors.New("Subscription data must contain a channel name")


This section is empty.


type Client

type Client struct {
	// contains filtered or unexported fields

Client is an ActionCable websocket client.

func NewClient

func NewClient(url string, connHdrFunc HeaderFunc) *Client

NewClient creates a new Client that connects to the provided url using the HTTP headers from connHdrFunc, which will be called once for each connection attempt. If connHdrFunc returns an error when called, that will cause the connection attempt to fail.

func (*Client) Close

func (c *Client) Close()

Close closes any active connection on the Client and stops it from making additional connections.

func (*Client) Send

func (c *Client) Send(channel, action string, data map[string]interface{}) <-chan error

Send an action on a channel. The "action" field of data will be overridden. The return channel will send an error if one is encountered, or will close if the send completes.

func (*Client) Subscribe

func (c *Client) Subscribe(channel string) (<-chan *EventOrErr, error)

Subscribe establishes a subscription to a specific channel. If the channel is already subscribed, ErrAlreadySubscribed is returned. Otherwise an subscription channel will return. If the subscription is rejected at any point (including after a future reconnect), the channel will be closed. It will also be closed if Unsubscribe is called for that subscription.

The channel will receive a confirm_subscription event anytime the subscription is confirmed (including every time the client reconnects) and will receive any events sent to that channel. Events may be missed during disconnects, so a confirm_subscription event might be a good time to resynchronize state that might be stale.

func (*Client) SubscribeWith

func (c *Client) SubscribeWith(params json.RawMessage) (<-chan *EventOrErr, error)

SubscribeWith subscribes to a channel giving parameters the params must contain a field "channel" containing the channe to subscribe to

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(channel string)

Unsubscribe stops subscribing to channel.

type Command

type Command struct {
	Command    string            `json:"command"`
	Data       json.RawMessage   `json:"data,omitempty"`
	Identifier CommandIdentifier `json:"identifier"`
	// contains filtered or unexported fields

Command is a command issued on a channel.

type CommandIdentifier

type CommandIdentifier struct {
	// Channel is the name of the channel.
	Channel string
	RawData json.RawMessage

CommandIdentifier identifies which Channel a Command occurs on.

func (*CommandIdentifier) MarshalJSON

func (c *CommandIdentifier) MarshalJSON() ([]byte, error)

MarshalJSON encodes the CommandIdentifier to JSON.

func (*CommandIdentifier) UnmarshalJSON

func (c *CommandIdentifier) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes the CommandIdentifier from JSON.

type Event

type Event struct {
	Type string `json:"type"`

	Message    json.RawMessage    `json:"message"`
	Data       json.RawMessage    `json:"data"`
	Identifier *CommandIdentifier `json:"identifier"`

Event is a subscription event received on a channel.

type EventOrErr

type EventOrErr struct {
	Event *Event
	Err   error

EventOrErr is an Event or error returned on a subscription channel.

type HeaderFunc

type HeaderFunc func() (*http.Header, error)

HeaderFunc is a function that returns HTTP headers or an error.

Source Files

Jump to

Keyboard shortcuts

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