https://github.com/lordpax/sockevent
A simple Go library for managing WebSocket connections, clients, and rooms inspired by the socket.io library.
https://github.com/lordpax/sockevent
golang socket-io websocket
Last synced: about 1 month ago
JSON representation
A simple Go library for managing WebSocket connections, clients, and rooms inspired by the socket.io library.
- Host: GitHub
- URL: https://github.com/lordpax/sockevent
- Owner: LordPax
- Created: 2024-07-30T21:21:36.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2025-11-16T01:01:14.000Z (7 months ago)
- Last Synced: 2025-11-16T03:05:40.296Z (7 months ago)
- Topics: golang, socket-io, websocket
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Sockevent
## Description
A simple Go library for managing WebSocket connections, clients, and rooms inspired by the socket.io library.
## Features
- WebSocket connection management
- Customizable event system
- Client management with data context
- Room creation and management
## Installation
```bash
go get github.com/LordPax/sockevent
```
## Example of project
- [golang-api-template](https://github.com/LordPax/golang-api-template/blob/master/websockets/websocket.go)
## Quick Start
```go
package main
import (
"net/http"
"github.com/LordPax/sockevent"
)
func main() {
ws := sockevent.GetWebsocket()
// Handle incoming messages
ws.On("message", func(client *sockevent.Client, message any) error {
// Process the message
return client.Emit("response", "Message received!")
})
// Set up HTTP handler for WebSocket
http.HandleFunc("/ws", ws.WsHandler)
// Start the server
http.ListenAndServe(":8080", nil)
}
```
## Detailed Features
### Connection Management
```go
ws := sockevent.GetWebsocket()
ws.OnConnect(func(client *sockevent.Client, w http.ResponseWriter, r *http.Request) error {
// Connection logic
return nil
})
ws.OnDisconnect(func(client *sockevent.Client) error {
// Disconnection logic
return nil
})
```
### Event Handling
```go
ws.On("customEvent", func(client *sockevent.Client, data any) error {
// Handle custom event
return nil
})
```
### Client Manipulation
```go
// Send a message to a client
client.Emit("event", "Data")
// Store data for a client
client.Set("userID", 123)
// Retrieve data from a client
userID := client.Get("userID")
```
### Room Management
```go
// Create or get a room
room := ws.Room("room-name")
// Add a client to a room
room.AddClient(client)
// Remove a client from a room
room.RemoveClient(client)
// Send a message to all clients in a room
room.Emit("announcement", "Message for everyone in the room")
```