Categorygithub.com/ao-concepts/websockets
modulepackage
0.11.8
Repository: https://github.com/ao-concepts/websockets.git
Documentation: pkg.go.dev

# README

ao-concepts websockets module

CI codecov

This module provides a websocket handler for usage with fiber. The handler is a abstraction layer that makes the handling of websocket messages easier.

Information

The ao-concepts ecosystem is still under active development and therefore the API of this module may have breaking changes until there is a first stable release.

If you are interested in contributing to this project, feel free to open a issue to discus a new feature, enhancement or improvement. If you found a bug or security vulnerability in this package, please start a issue, or open a PR against master.

Installation

go get -u github.com/ao-concepts/websockets

Usage

log := logging.New(logging.Debug, nil)
server, err := websockets.New(nil, log)
if err != nil {
    log.ErrFatal(err)
}

// batch sent messages. This can be used to reduce load on clients.
// Batched message will be prefixed by `batch_`.
// The data will be stored as array of batched payloads below the `d` property of the actual sent message.
if err := server.UseBatch("event:name", time.Second); err != nil {
	log.ErrError(err)
}

server.Subscribe("event:name", func(msg *websockets.Message) {
    // here you can handle the message
    c := msg.Connection

    // access the payload
    fmt.Println(msg.Payload["value"])

    // you can set data to the current session
    c.Set("key", "value")
    c.Get("key")

    // you can respond directly on the connection that received the message.
    c.SendMessage(&websockets.Message{
        Event:   "event:name",
        Payload: websockets.Payload{
            "value": "data",
        },
    })

    // and you can respond to all connections that match a filter.
    c.SendMessage(&websockets.Message{
        Event:   "event:name",
        Payload: websockets.Payload{
            "value": "data",
        },
    }, func (c *websockets.Connection) bool {
        return c.Get("key") == true
    })
})

app := fiber.New()
app.Get("/ws", s.Handler)
app.Listen(":3000")

Used packages

This project uses some really great packages. Please make sure to check them out!

PackageUsage
github.com/ao-concepts/eventbusPersistence helper
github.com/gofiber/fiberHTTP router
github.com/jasonlvhit/gocronBatch messages
github.com/stretchr/testifyTesting

# Functions

New server constructor.
NewBatch constructor.
NewConnection constructor.
NewWebsocketConnMock constructor.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
Connection on a websocket.
Message that is received or sent via a websocket.
Server for websockets.
ServerConfig configuration of the server.
WebsocketConnMock mock struct for websocket connections You can set `DoError` to let any method that can return an error return errors.

# Interfaces

Logger interface.
WebsocketConn websocket connection interface.

# Type aliases

Filter function that checks if a connection matches some criteria.
OnConnectionClose is executed when a connection is closed.
No description provided by the author
Payload send by a websocket connection.