resilientws

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2025 License: MIT Imports: 12 Imported by: 0

README

Resilient WebSocket Client for Go

Status Go GitHub license

resilientws is a lightweight and resilient WebSocket client library built in Go, designed for real-time data streaming applications that require automatic reconnection, subscription resumption, and heartbeat management.

Feature

  • Connection Management — Establish and maintain a resilient WebSocket connection using the provided configuration (Headers, TLSConfig, Proxy, Logger, PingInterval, PongTimeout, MessageQueueSize).
  • Automatic Reconnection — Automatically reconnect to the WebSocket server if the connection is lost, with configurable retry intervals.
  • Subscribe Handler — Invoke the SubscribeHandler function after each successful connection or reconnection event.
  • Message Handler — Handle incoming messages from the WebSocket server.
  • Ping/Pong Mechanism — Send ping messages at the configured PingInterval to keep the connection alive, using the PingHandler function.
  • Event Handling — Provide callbacks for connection, reconnection, and error events (onConnected, onReconnecting, onError).
  • Message Queue — Maintain a queue of messages to be sent when the connection is reestablished after a disconnection.

Installation

go get github.com/ipanardian/resilientws

Usage

Check the example directory for usage examples.

License

resilientws is licensed under the MIT License.

resilientws inspired by recws

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffType added in v0.1.1

type BackoffType int
const (
	BackoffTypeJitter BackoffType = iota
	BackoffTypeFixed
)

type Event

type Event struct {
	Type        EventType
	Message     []byte
	MessageType int
	Data        interface{}
	Error       error
}

type EventType

type EventType int
const (
	EventMessage EventType = iota
	EventConnected
	EventReconnecting
	EventError
	EventClose
)

type Logger

type Logger interface {
	Debug(msg string, args ...interface{})
	Info(msg string, args ...interface{})
	Error(msg string, args ...interface{})
}

type Resws

type Resws struct {
	// RecBackoffMin is the minimum backoff duration between reconnection attempts
	RecBackoffMin time.Duration

	// RecBackoffMax is the maximum backoff duration between reconnection attempts
	RecBackoffMax time.Duration

	// RecBackoffFactor is the factor by which the backoff duration is multiplied
	RecBackoffFactor float64

	// BackoffType is the type of backoff to use
	BackoffType BackoffType

	// Handshake timeout
	HandshakeTimeout time.Duration

	// Headers to be sent with the connection
	Headers http.Header

	// Ping interval
	PingInterval time.Duration

	// Pong timeout
	PongTimeout time.Duration

	// Read deadline
	ReadDeadline time.Duration

	// Write deadline
	WriteDeadline time.Duration

	// Message queue size
	MessageQueueSize int

	// TLS configuration
	TLSConfig *tls.Config

	// Proxy configuration
	Proxy func(*http.Request) (*url.URL, error)

	// Logger
	Logger Logger

	// Non-verbose mode
	NonVerbose bool

	// Subscribe handler
	SubscribeHandler func() error

	// Message handler
	MessageHandler func(int, []byte)

	// Ping handler
	PingHandler func()

	*websocket.Conn
	// contains filtered or unexported fields
}

func (*Resws) Close

func (r *Resws) Close()

Close closes the connection

func (*Resws) CloseAndReconnect

func (r *Resws) CloseAndReconnect()

CloseAndReconnect closes the connection and starts a reconnection attempt

func (*Resws) Dial

func (r *Resws) Dial(url string)

Dial establishes a connection to the WebSocket server

func (*Resws) GetHTTPResponse

func (r *Resws) GetHTTPResponse() *http.Response

GetHTTPResponse returns the HTTP response

func (*Resws) IsConnected

func (r *Resws) IsConnected() bool

IsConnected returns the connection state

func (*Resws) LastConnectTime

func (r *Resws) LastConnectTime() time.Time

LastConnectTime returns the last connection time

func (*Resws) LastError

func (r *Resws) LastError() error

LastError returns the last error

func (*Resws) OnConnected

func (r *Resws) OnConnected(fn func(url string))

OnConnected sets the connected handler

func (*Resws) OnError

func (r *Resws) OnError(fn func(err error))

OnError sets the error handler

func (*Resws) OnReconnecting

func (r *Resws) OnReconnecting(fn func(time.Duration))

OnReconnecting sets the reconnection handler

func (*Resws) ReadJSON

func (r *Resws) ReadJSON(v any) (err error)

ReadJSON manually reads a JSON message from the websocket connection

func (*Resws) ReadMessage

func (r *Resws) ReadMessage() (msgType int, msg []byte, err error)

ReadMessage manually reads a message from the websocket connection

func (*Resws) Send

func (r *Resws) Send(msg []byte) error

Send sends a message to the WebSocket server with a fallback queue

func (*Resws) SendJSON

func (r *Resws) SendJSON(v any) (err error)

func (*Resws) WriteJSON

func (r *Resws) WriteJSON(v any) (err error)

WriteJSON manually writes a JSON message to the websocket connection

func (*Resws) WriteMessage

func (r *Resws) WriteMessage(msgType int, msg []byte) (err error)

WriteMessage manually writes a message to the websocket connection

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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