sockets

package module
v0.0.12-beta Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 8 Imported by: 6

README

sockets

Channel based socket library on top of gorilla/sockets giving listeners, middleware, server and clients.

Release Build Status CodeQL Status Report codecov Go

Table of Contents

Installation

sockets requires a supported release of Go.

go get -u github.com/theflyingcodr/sockets

Documentation

View the generated documentation

GoDoc

For more information around the technical aspects of Bitcoin, please see the updated Bitcoin Wiki

Features
  • Client / Server implementations
  • Middleware
  • Error handlers
  • Listener funcs
  • Channel based messaging
  • Client reconnects
  • Broadcasting directly from server to client or to entire channel
Library Deployment

goreleaser for easy binary or library deployment to Github and can be installed via: brew install goreleaser.

The .goreleaser.yml file is used to configure goreleaser.

Use make release-snap to create a snapshot version of the release, and finally make release to ship to production.

Examples & Tests

All unit tests and examples run via Github Actions and uses Go version 1.17.x. View the configuration file.

Run all tests

make pre-commit

Usage

View the examples


Maintainers

JH
MS

Contributing

View the contributing guidelines and please follow the code of conduct.

How can I help?

All kinds of contributions are welcome 🙌! The most basic way to show your support is to star 🌟 the project, or to raise issues 💬. You can also support this project by becoming a sponsor on GitHub 👏


License

License

Documentation

Index

Constants

View Source
const (
	MessageJoinSuccess    = "join.success"
	MessageLeaveSuccess   = "leave.success"
	MessageGetInfo        = "get.info"
	MessageInfo           = "info"
	MessageError          = "error"
	MessageChannelExpired = "channel.expired"
	MessageChannelClosed  = "channel.closed"
)

Common messages.

View Source
const (
	HeaderChannelExpiry = "X-Channel-Expires"
)

common headers.

Variables

View Source
var ErrChannelNotFound = errors.New("channel not found")

ErrChannelNotFound returned when channel is not found.

Functions

This section is empty.

Types

type Client

type Client interface {
	JoinChannel(host, channelID string, headers http.Header, params map[string]string) error
	LeaveChannel(channelID string, headers http.Header)
	RegisterListener(msgType string, fn HandlerFunc)
}

Client can be used to implement a client which will send and listen to messages on channels.

type ClientErrorHandlerFunc

type ClientErrorHandlerFunc func(err error, msg *Message)

ClientErrorHandlerFunc is raised when the client itself returns an error when processing a message. It can be logged, retried etc.

type ClientErrorMsgHandlerFunc

type ClientErrorMsgHandlerFunc func(err ErrorMessage)

ClientErrorMsgHandlerFunc is triggered when a server returns an error message in response to a client message. It will contain the detail of the error and the client can decide to log, re-send or do something else.

type ClientPublisher

type ClientPublisher interface {
	Publish(req Request)
}

ClientPublisher can be implemented to allow a client to send messages to a connected server and channel.

type ErrorDetail

type ErrorDetail struct {
	Title       string
	Description string
	ErrCode     string
}

ErrorDetail is returned as the message body in the event of an error.

type ErrorMessage

type ErrorMessage struct {
	CorrelationID string          `json:"correlationId"`
	AppID         string          `json:"appId"`
	UserID        string          `json:"userId"`
	Key           string          `json:"type"`
	OriginKey     string          `json:"originType"`
	OriginBody    json.RawMessage `json:"originBody"`
	ErrorBody     json.RawMessage `json:"errorBody"`
	ChannelID     string          `json:"channelId"`
	ClientID      string          `json:"clientId"`
	Headers       http.Header     `json:"headers"`
}

ErrorMessage is a message type returned on error, it contains a copy of the original key and body but will have a key of "error" allowing this to be checked for in error handlers. ErrorBody is optional but if supplied can be marhsaled to a struct using Bind() to get the detail of the error.

func (*ErrorMessage) Bind

func (e *ErrorMessage) Bind(v interface{}) error

Bind will decode the error message body to v. This message will usually contain further error data and can be specified by the server.

func (*ErrorMessage) BindOriginBody

func (e *ErrorMessage) BindOriginBody(v interface{}) error

BindOriginBody can be used to decode the body for the original message that triggered this error, useful for replaying.

type HandlerFunc

type HandlerFunc func(ctx context.Context, msg *Message) (*Message, error)

HandlerFunc defines listeners on both the server and clients. When registered these will be triggered when a message is received matching the key.

type Message

type Message struct {
	CorrelationID string
	AppID         string
	UserID        string
	Expiration    *time.Time
	Body          json.RawMessage

	Headers  http.Header
	ClientID string
	// contains filtered or unexported fields
}

Message is the underlying message type used by the protocol to transmit metadata and the message bodies.

func NewMessage

func NewMessage(msgType, clientID, channelID string) *Message

NewMessage will create a new message setting.

func (Message) Bind

func (m Message) Bind(v interface{}) error

Bind will map the body to v.

func (*Message) ChannelID

func (m *Message) ChannelID() string

ChannelID returns the message channelID.

func (*Message) ID

func (m *Message) ID() string

ID returns the message unique identifier.

func (*Message) Key

func (m *Message) Key() string

Key returns the message key.

func (*Message) MarshalJSON

func (m *Message) MarshalJSON() ([]byte, error)

MarshalJSON implements the json Marshaller.

func (*Message) NewFrom

func (m *Message) NewFrom(key string) *Message

NewFrom will take a copy of msg and return a new message from it.

You can then add a body using the WithBody func and add headers etc.

func (*Message) NoContent

func (m *Message) NoContent() (*Message, error)

NoContent is a helper that can be used to return an empty message from a listener.

func (*Message) Timestamp

func (m *Message) Timestamp() time.Time

Timestamp returns the message created at date.

func (*Message) ToError

func (m *Message) ToError(err interface{}) *ErrorMessage

ToError will convert a message to an ErrorMessage with an optional err struct supplied containing additional details for the error.

There is a default sockets.ErrorDetail struct available, or you can define your own.

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(bb []byte) error

UnmarshalJSON implements the json unmarshaler.

func (*Message) WithBody

func (m *Message) WithBody(v interface{}) error

WithBody will serialise the value v into the message body.

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

MiddlewareFunc defines a common middleware signature that can be used to create middleware that executes before the HandlerFunc.

type Publisher

type Publisher interface {
	Publish(req Request) error
}

Publisher is used by clients to send messages to a server.

type Request

type Request struct {
	ChannelID  string
	MessageKey string
	Body       interface{}
	Headers    http.Header
}

Request is used to send a message to a channel with a specific key.

type ServerChannelBroadcaster

type ServerChannelBroadcaster interface {
	// Broadcast will send a message to all connected peers on a channel.
	// To handle these messages you should register a handler using server.RegisterChannelHandler.
	Broadcast(channelID string, msg *Message)
	// BroadcastAwait will broadcast a message to all peers but handle only the first one and return
	// it to the caller. This is a blocking call.
	BroadcastAwait(ctx context.Context, channelID string, msg *Message) (*Message, error)
}

ServerChannelBroadcaster is used to send a message from a server to all clients connected to a channel.

type ServerDirectBroadcaster

type ServerDirectBroadcaster interface {
	BroadcastDirect(clientID string, msg *Message)
}

ServerDirectBroadcaster is used to send a message from a server directly to a client.

type ServerErrorHandlerFunc

type ServerErrorHandlerFunc func(msg *Message, e error) *ErrorMessage

ServerErrorHandlerFunc defines an error handler for a server.

The message can be handled, logged, and returned to clients.

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

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