ws

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package ws contains several tools for dealing with websockets such as easily setting these up error handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorResponse

func ErrorResponse(
	ctx context.Context,
	conn *websocket.Conn,
	status int,
	message any,
)

ErrorResponse is used to handle any kind of error that occurred on a WebSocket.

func FailedValidationResponse

func FailedValidationResponse(
	ctx context.Context,
	conn *websocket.Conn,
	errors map[string]string,
)

FailedValidationResponse is used to handle an error of a validate.Validator that occurred on a WebSocket.

func ForbiddenResponse

func ForbiddenResponse(ctx context.Context, conn *websocket.Conn)

ForbiddenResponse is used to handle an error when a user isn't authorized to access a certain resource.

func ServerErrorResponse

func ServerErrorResponse(ctx context.Context, conn *websocket.Conn, err error)

ServerErrorResponse is used to handle internal server errors that occurred on a WebSocket.

func UpgradeErrorResponse

func UpgradeErrorResponse(w http.ResponseWriter, r *http.Request, err error)

UpgradeErrorResponse is used to handle an error that occurred during the upgrade towards a WebSocket.

Types

type OnSubscribeCallback

type OnSubscribeCallback = func(ctx context.Context, topic *Topic) (any, error)

OnSubscribeCallback is called to fetch data that should be returned when a new subscriber is added to a topic.

type SubscribeMessageDto

type SubscribeMessageDto interface {
	validate.ValidatedType
	Topic() string
}

SubscribeMessageDto is implemented by all messages used to subscribe to a certain handler of a WebSocketHandler.

type Subscriber

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

Subscriber is used to receive messages from a Topic and managed the websocket.Conn.

func NewSubscriber

func NewSubscriber(topic *Topic, conn *websocket.Conn) Subscriber

NewSubscriber returns a new Subscriber.

func (Subscriber) ID

func (sub Subscriber) ID() string

ID returns the id of a Subscriber.

func (Subscriber) OnEventCallback

func (sub Subscriber) OnEventCallback(event any)

OnEventCallback is called when a new event is pushed to Subscriber. If the connection would be closed, [UnSubscribe] will be called.

type Topic

type Topic struct {
	Name string
	// contains filtered or unexported fields
}

Topic is used to efficiently send messages to [Subscriber]s in a WebSocket.

func NewTopic

func NewTopic(
	logger *slog.Logger,
	name string,
	allowedOrigins []string,
	maxWorkers int,
	channelBufferSize int,
	onSubscribeCallback OnSubscribeCallback,
) *Topic

NewTopic creates a new Topic.

func (*Topic) EnqueueEvent

func (t *Topic) EnqueueEvent(event any)

EnqueueEvent enqueues an event if there are subscribers on this Topic.

func (*Topic) Subscribe

func (t *Topic) Subscribe(conn *websocket.Conn) error

Subscribe subscribes a Subscriber to this Topic. If configured a message will be sent on subscribing. If no message handling go routine was running this will be started now.

func (*Topic) UnSubscribe

func (t *Topic) UnSubscribe(sub Subscriber)

UnSubscribe unsubscribes a Subscriber from this Topic.

type WebSocketHandler

type WebSocketHandler[T SubscribeMessageDto] struct {
	// contains filtered or unexported fields
}

A WebSocketHandler handles incoming requests to a websocket and makes sure subscriptions are made to the right topics.

func CreateWebSocketHandler

func CreateWebSocketHandler[T SubscribeMessageDto](
	logger *slog.Logger,
	maxTopicWorkers int,
	topicChannelBufferSize int,
) WebSocketHandler[T]

CreateWebSocketHandler creates a new WebSocketHandler.

func (*WebSocketHandler[T]) AddTopic

func (h *WebSocketHandler[T]) AddTopic(
	topicName string,
	allowedOrigins []string,
	onSubscribeCallback OnSubscribeCallback,
) (*Topic, error)

AddTopic adds a topic to which can be subscribed using a SubscribeMessageDto. The onSubscribeCallback is called for each new subscriber to fetch data to send them back.

func (WebSocketHandler[T]) Handler

func (h WebSocketHandler[T]) Handler() http.HandlerFunc

Handler returns the http.HandlerFunc of a WebSocketHandler.

func (*WebSocketHandler[T]) RemoveTopic

func (h *WebSocketHandler[T]) RemoveTopic(topic *Topic) error

RemoveTopic removes a topic to which can be subscribed using a SubscribeMessageDto.

func (*WebSocketHandler[T]) UpdateTopicName

func (h *WebSocketHandler[T]) UpdateTopicName(
	topic *Topic,
	newName string,
) (*Topic, error)

UpdateTopicName updates the name of a topic without losing its subscribers.

Jump to

Keyboard shortcuts

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