ikisocket

package module
v0.0.0-...-a764a30 Latest Latest
Warning

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

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

README

WebSocket wrapper for Fiber v2 with events support

Go Report Card GoDoc GitHub license

Based on Fiber Websocket and inspired by Socket.io
Upgrade to Fiber v2 details here

Any bug?

Create ad issue following this template

Feature request?

Create ad issue following this template

⚙️ Installation

go get -u github.com/sculas/ikisocket

📖 ️ Documentation

// Initialize new ikisocket in the callback this will
// execute a callback that expects kws *Websocket Object
New(callback func(kws *Websocket)) func(*fiber.Ctx) error

// Add listener callback for an event into the listeners list
On(event string, callback func(payload *EventPayload))

ikisocket public/global functions (many thanks to Daniel Morawetz)

// Emit the message to a specific socket uuids list
// Ignores all errors
EmitToList(uuids []string, message []byte)

// Emit to a specific socket connection
EmitTo(uuid string, message []byte) error

// Broadcast to all the active connections
// except avoid broadcasting the message to itself
Broadcast(message []byte)

// Fire custom event on all connections
Fire(event string, data []byte) 

Supported events:

// Supported event list
const (
	// Fired when a Text/Binary message is received
	EventMessage = "message"
	// More details here:
	// @url https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Pings_and_Pongs_The_Heartbeat_of_WebSockets
	EventPing = "ping"
	EventPong = "pong"
	// Fired on disconnection
	// The error provided in disconnection event
	// is defined in RFC 6455, section 11.7.
	// @url https://github.com/gofiber/websocket/blob/cd4720c435de415b864d975a9ca23a47eaf081ef/websocket.go#L192
	EventDisconnect = "disconnect"
	// Fired on first connection
	EventConnect = "connect"
	// Fired when the connection is actively closed from the server
	EventClose = "close"
	// Fired when some error appears useful also for debugging websockets
	EventError = "error"
)

Event Payload object

// Event Payload is the object that
// stores all the information about the event and
// the connection
type EventPayload struct {
	// The connection object
	Kws *Websocket
	// The name of the event
	Name string
	// Unique connection UUID
	SocketUUID string
	// Optional websocket attributes
	SocketAttributes map[string]string
	// Optional error when are fired events like
	// - Disconnect
	// - Error
	Error error
	// Data is used on Message and on Error event
	Data []byte
}

Socket instance functions

// Set a specific attribute for the specific socket connection
func (kws *Websocket) SetAttribute(key string, attribute string)

// Get socket connection UUID
func (kws *Websocket) GetUUID() string

// Set socket connection UUID
func (kws *Websocket) SetUUID(uuid string)

// Set socket connection UUID
func (kws *Websocket) SetUUID(uuid string)

// Get a specific attribute from the socket attributes
func (kws *Websocket) GetAttribute(key string) string

// Emit the message to a specific socket uuids list
func (kws *Websocket) EmitToList(uuids []string, message []byte) 

// Emit to a specific socket connection
func (kws *Websocket) EmitTo(uuid string, message []byte) error

// Broadcast to all the active connections
// except avoid broadcasting the message to itself
func (kws *Websocket) Broadcast(message []byte, except bool)

// Fire custom event
func (kws *Websocket) Fire(event string, data []byte)

// Emit/Write the message into the given connection
func (kws *Websocket) Emit(message []byte)

// Actively close the connection from the server
func (kws *Websocket) Close() 

⚡️ Examples

Documentation

Index

Constants

View Source
const (
	// TextMessage denotes a text data message. The text message payload is
	// interpreted as UTF-8 encoded text data.
	TextMessage = 1
	// BinaryMessage denotes a binary data message.
	BinaryMessage = 2
	// CloseMessage denotes a close control message. The optional message
	// payload contains a numeric code and text. Use the FormatCloseMessage
	// function to format a close message payload.
	CloseMessage = 8
	// PingMessage denotes a ping control message. The optional message payload
	// is UTF-8 encoded text.
	PingMessage = 9
	// PongMessage denotes a pong control message. The optional message payload
	// is UTF-8 encoded text.
	PongMessage = 10
)

Source @url:https://github.com/gorilla/websocket/blob/master/conn.go#L61 The message types are defined in RFC 6455, section 11.8.

View Source
const (
	// EventMessage Fired when a Text/Binary message is received
	EventMessage = "message"
	// EventPing More details here:
	// @url https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Pings_and_Pongs_The_Heartbeat_of_WebSockets
	EventPing = "ping"
	EventPong = "pong"
	// EventDisconnect Fired on disconnection
	// The error provided in disconnection event
	// is defined in RFC 6455, section 11.7.
	// @url https://github.com/gofiber/websocket/blob/cd4720c435de415b864d975a9ca23a47eaf081ef/websocket.go#L192
	EventDisconnect = "disconnect"
	// EventConnect Fired on first connection
	EventConnect = "connect"
	// EventClose Fired when the connection is actively closed from the server
	EventClose = "close"
	// EventError Fired when some error appears useful also for debugging websockets
	EventError = "error"
)

Supported event list

Variables

View Source
var (
	// ErrorInvalidConnection The addressed ws connection is not available anymore
	// error data is the uuid of that connection
	ErrorInvalidConnection = errors.New("message cannot be delivered invalid/gone connection")
	// ErrorUUIDDuplication The UUID already exists in the pool
	ErrorUUIDDuplication = errors.New("UUID already exists in the available connections pool")
)
View Source
var (
	PongTimeout = 1 * time.Second
	// RetrySendTimeout retry after 20 ms if there is an error
	RetrySendTimeout = 20 * time.Millisecond
	//MaxSendRetry define max retries if there are socket issues
	MaxSendRetry = 5
	// ReadTimeout Instead of reading in a for loop, try to avoid full CPU load taking some pause
	ReadTimeout = 10 * time.Millisecond
)

Functions

func Broadcast

func Broadcast(message []byte)

Broadcast to all the active connections

func EmitTo

func EmitTo(uuid string, message []byte) error

EmitTo emits to a specific socket connection

func EmitToList

func EmitToList(uuids []string, message []byte)

EmitToList Emit the message to a specific socket uuids list Ignores all errors

func Fire

func Fire(event string, data []byte)

Fire custom event on all connections

func New

func New(callback func(kws *Websocket)) func(*fiber.Ctx) error

func On

func On(event string, callback eventCallback)

On Add listener callback for an event into the listeners list

Types

type EventPayload

type EventPayload struct {
	// The connection object
	Kws *Websocket
	// The name of the event
	Name string
	// Unique connection UUID
	SocketUUID string
	// Optional websocket attributes
	SocketAttributes map[string]interface{}
	// Optional error when are fired events like
	// - Disconnect
	// - Error
	Error error
	// Data is used on Message and on Error event
	Data []byte
}

EventPayload Event Payload is the object that stores all the information about the event and the connection

type Websocket

type Websocket struct {

	// Unique id of the connection
	UUID string
	// Wrap Fiber Locals function
	Locals func(key string) interface{}
	// Wrap Fiber Params function
	Params func(key string, defaultValue ...string) string
	// Wrap Fiber Query function
	Query func(key string, defaultValue ...string) string
	// Wrap Fiber Cookies function
	Cookies func(key string, defaultValue ...string) string
	// contains filtered or unexported fields
}

func Get

func Get(uuid string) (*Websocket, error)

func (*Websocket) Broadcast

func (kws *Websocket) Broadcast(message []byte, except bool)

Broadcast to all the active connections except avoid broadcasting the message to itself

func (*Websocket) Close

func (kws *Websocket) Close()

Close closes the connection from the server

func (*Websocket) CloseWith

func (kws *Websocket) CloseWith(message []byte, closeCode int)

CloseWith closes the connection from the server with a message

func (*Websocket) Emit

func (kws *Websocket) Emit(message []byte)

Emit writes the message into the given connection

func (*Websocket) EmitTo

func (kws *Websocket) EmitTo(uuid string, message []byte) error

EmitTo Emit to a specific socket connection

func (*Websocket) EmitToList

func (kws *Websocket) EmitToList(uuids []string, message []byte)

EmitToList Emit the message to a specific socket uuids list

func (*Websocket) Fire

func (kws *Websocket) Fire(event string, data []byte)

Fire custom event

func (*Websocket) GetAttribute

func (kws *Websocket) GetAttribute(key string) interface{}

GetAttribute Get a specific attribute from the socket attributes

func (*Websocket) GetIntAttribute

func (kws *Websocket) GetIntAttribute(key string) int

GetIntAttribute Convenience method to retrieve an attribute as an int. Will panic if attribute is not an int.

func (*Websocket) GetStringAttribute

func (kws *Websocket) GetStringAttribute(key string) string

GetStringAttribute Convenience method to retrieve an attribute as a string. Will panic if attribute is not an int.

func (*Websocket) GetUUID

func (kws *Websocket) GetUUID() string

func (*Websocket) IsAlive

func (kws *Websocket) IsAlive() bool

func (*Websocket) SetAttribute

func (kws *Websocket) SetAttribute(key string, attribute interface{})

SetAttribute Set a specific attribute for the specific socket connection

func (*Websocket) SetUUID

func (kws *Websocket) SetUUID(uuid string)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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