Giter Club home page Giter Club logo

ikisocket's Introduction

WebSocket wrapper for Fiber v2 with events support

Go Report Card GoDoc GitHub license Build status

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/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
}

FastHTTP Websocket connection exposed Request #30

type Websocket struct {
    // The FastHTTP connection
    Conn *websocket.Conn
}

Can be accessed from

kws.Conn

This will allow direct access to the following FastHTTP functions

FastHTTP Connection 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() 

⚡️ Examples

ikisocket's People

Contributors

antoniodipinto avatar dmorawetz avatar dependabot[bot] avatar yiyuhki avatar jibon57 avatar whizdummy avatar kayode0x avatar ramiberm avatar crgao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.