https://github.com/aliforever/go-socketify
Golang WebSocket Framework
https://github.com/aliforever/go-socketify
framework go go-websocket golang socketify websocket websocket-client websocket-framework websocket-server ws
Last synced: 13 days ago
JSON representation
Golang WebSocket Framework
- Host: GitHub
- URL: https://github.com/aliforever/go-socketify
- Owner: aliforever
- License: mit
- Created: 2022-01-15T22:15:43.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-17T12:39:07.000Z (about 3 years ago)
- Last Synced: 2024-06-19T06:56:21.781Z (over 1 year ago)
- Topics: framework, go, go-websocket, golang, socketify, websocket, websocket-client, websocket-framework, websocket-server, ws
- Language: Go
- Homepage:
- Size: 60.5 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go-Socketify
A simple WebSocket framework for Go
## Install
```go get -u github.com/aliforever/go-socketify```
## Usage
A simple app that PONG when PING
```go
options := socketify.ServerOptions().SetAddress(":8080").SetEndpoint("/ws").IgnoreCheckOrigin()
server := socketify.NewServer(options)
go server.Listen()
for connection := range server.Connections() {
connection.HandleUpdate("PING", socketify.NewMapper[socketify.EmptyInput](func(_ socketify.EmptyInput) {
connection.WriteUpdate("PONG", nil)
}))
go connection.ProcessUpdates()
}
```
Run the application and send below JSON to "ws://127.0.0.1:8080/ws":
```json
{
"type": "PING"
}
```
You'll receive:
```json
{
"type": "PONG"
}
```
## Conventions
Events are ought to be sent/received with following JSON format:
```json
{
"type": "UpdateType",
"data": {}
}
```
Type is going to be your update type and data is going to be anything.
## Storage
You can retrieve clients within other clients by using Socketify's client storage.
You can enable the storage by setting option `EnableStorage()`:
```go
options := socketify.Options().
SetAddress(":8080").
SetEndpoint("/ws").
IgnoreCheckOrigin().
EnableStorage() // <-- This LINE
```
Each client has a unique ID set by [shortid](github.com/teris-io/shortid) package, you can recall using `client.ID()`.
Clients are stored in a map with their unique ID and you can retrieve them by calling:
```go
client.Server().Storage().GetClientByID(UniqueID)
```
## Handlers
You can specify a handler for an `updateType` to each client by using:
```go
client.HandleUpdate("UpdateType", func(message json.RawMessage) {
// Process message
})
```
This way Socketify will call your registered handler if it receives any updates with `UpdateType` specified.
Or you can just listen on updates on your own:
```go
go client.ProcessUpdates()
go func(c *socketify.Client) {
for update := range c.Updates() {
fmt.Println(update)
}
}(client)
```
Note: You should always call `go client.ProcessUpdates()` to let a Socketify client receive updates.
## Docs:
Checkout Docs [Here](https://pkg.go.dev/github.com/aliforever/go-socketify)