Fiber v2 with events support
WebSocket wrapper forFiber Websocket and inspired by Socket.io
Based onhere
Upgrade to Fiber v2 detailsAny bug?
Create ad issue following this template
Feature request?
Create ad issue following this template
⚙️ Installation
go get -u github.com/antoniodipinto/ikisocket
Documentation
📖 ️// Initialize new ikisocket in the callback this will
// execute a callback that expects kws *Websocket Object
func New(callback func(kws *Websocket)) func(*fiber.Ctx) error
// Add listener callback for an event into the listeners list
func 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
func EmitToList(uuids []string, message []byte)
// Emit to a specific socket connection
func EmitTo(uuid string, message []byte) error
// Broadcast to all the active connections
// except avoid broadcasting the message to itself
func Broadcast(message []byte)
// Fire custom event on all connections
func 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
}
Request #30
FastHTTP Websocket connection exposedtype Websocket struct {
// The FastHTTP connection
Conn *websocket.Conn
}
Can be accessed from
kws.Conn
This will allow direct access to the following FastHTTP functions
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)
// 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()