aria

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 5 Imported by: 0

README

Aria

Aria is a lightweight, event-driven WebSocket framework for Go. Aria is inspired by olahol/melody, and based on coder/websocket. It provides a simple API for managing WebSocket connections, broadcasting messages, and handling events like OnConnect, OnMessage, OnClose, and OnDisconnect.

The goal of Aria is to make building real-time applications (chat servers, dashboards, games, etc.) as straightforward as possible while keeping performance and code clarity.


Features

  • ✅ Simple API inspired by event-driven frameworks
  • ✅ Hook functions: OnConnect, OnMessage, OnClose, OnDisconnect, OnMessageBinary, OnError
  • ✅ Broadcast support (BroadCast and BroadCastFilter)
  • ✅ Connection lifecycle management with graceful cleanup
  • ✅ Context-aware connection handling (Handle and HandleWithContext)
  • ✅ Support for WebSocket compression and subprotocols via options

Installation

go get github.com/n9te9/aria

Quick Start

See the _example/chat directory for a complete chat application using Aria. This includes both a Go WebSocket server and a simple HTML/JavaScript client.

Contribution

Contributions are welcome! 🎉 If you’d like to improve Aria, please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Write your code and add/update tests
  4. Run go test ./... to ensure all tests pass
  5. Open a Pull Request with a clear description of your changes

Please also check for consistency in naming conventions, comments, and code style before submitting.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) *aria

New initialize aria websocket framework instance. New can setup option by Functional Option Partten. Option func is provided as WithXxx function.

Types

type Aria

type Aria interface {
	BroadCast(ctx context.Context, message []byte) error
	BroadCastFilter(ctx context.Context, message []byte, filter FilterFunc) error

	HandleWithContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error
	Handle(w http.ResponseWriter, r *http.Request) error

	OnConnect(func(ctx context.Context, conn *Conn) error)
	OnMessage(func(ctx context.Context, conn *Conn, message []byte) error)
	OnMessageBinary(func(ctx context.Context, conn *Conn, message []byte) error)
	OnDisconnect(func(ctx context.Context, conn *Conn) error)
	OnClose(func(ctx context.Context, conn *Conn) error)
	OnError(func(ctx context.Context, conn *Conn, err error))
}

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func (*Conn) Delete

func (c *Conn) Delete(key string)

Delete can delete stored value that is set by Set method.

func (*Conn) Get

func (c *Conn) Get(key string) (value any, ok bool)

Get can retrive value that is set by Set method.

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) error

Ping can ping for the connection.

func (*Conn) Set

func (c *Conn) Set(key string, value any)

Set can store any value.

type Conns

type Conns map[*Conn]struct{}

func (Conns) Remove

func (c Conns) Remove(conn *Conn)

Remove delete connection with argument connection.

func (Conns) Slice

func (c Conns) Slice() []*Conn

Slice convert map[*Conn]struct{} to []*Conn

type FilterFunc

type FilterFunc func(conns *Conn) bool

type Option

type Option interface {
	Apply(*aria)
}

Option represents a configuration option for an aria instance. Options can be passed to New() to customize server behavior.

func WithComporessionMode

func WithComporessionMode(mode int) Option

WithComporessionMode sets the compression mode for WebSocket messages. Use values from websocket.CompressionMode.

func WithCompressionThreshold

func WithCompressionThreshold(threshold int) Option

WithCompressionThreshold sets the minimum message size (in bytes) required before compression is applied.

func WithInsecureSkipVerify

func WithInsecureSkipVerify(skip bool) Option

WithInsecureSkipVerify configures whether to skip TLS certificate verification during the WebSocket handshake. This is useful for development or when using self-signed certificates.

func WithOnPingReceived

func WithOnPingReceived(fn func(ctx context.Context, payload []byte) bool) Option

WithOnPingReceived registers a handler for incoming Ping frames. The callback return value determines whether the library should automatically respond with a Pong frame (true = auto respond).

func WithOnPongReceived

func WithOnPongReceived(fn func(ctx context.Context, payload []byte)) Option

WithOnPongReceived registers a handler for incoming Pong frames.

func WithOriginPatterns

func WithOriginPatterns(patterns ...string) Option

WithOriginPatterns sets the allowed origin patterns for validating the WebSocket client's Origin header.

func WithSubprotocols

func WithSubprotocols(protocols ...string) Option

WithSubprotocols specifies the WebSocket subprotocols that the server is willing to accept during the handshake.

Directories

Path Synopsis
_example
chat command
notification command

Jump to

Keyboard shortcuts

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