https://github.com/epikoder/nsocket
Event driven Messaging broker using native WebSocket
https://github.com/epikoder/nsocket
event messaging socket websocket
Last synced: 5 months ago
JSON representation
Event driven Messaging broker using native WebSocket
- Host: GitHub
- URL: https://github.com/epikoder/nsocket
- Owner: epikoder
- License: bsd-3-clause
- Created: 2022-10-14T17:25:45.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-18T10:47:31.000Z (over 3 years ago)
- Last Synced: 2024-06-20T12:45:03.254Z (about 2 years ago)
- Topics: event, messaging, socket, websocket
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NSocket
## Messaging framework with channels [rooms]
Inspired by [Socket.io](https://socket.io) and [Kataras Neffos](https://github.com/kataras/neffos)
### Installation
```bash
go get github.com/epikoder/nsocket
```
### Usage
```go
events := nsocket.Event{
"/": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
fmt.Printf("Namespace: [Default] -- GOT: %v ---- from ---- %v\n", i, s.RemoteAddr())
if err := soc.Broadcast("namespace:default -- " + fmt.Sprintf("%v ------> %v", i, s.RemoteAddr())); err != nil {
t.Error(err)
}
},
"message": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
fmt.Printf("Namespace: [Default/Message] -- GOT: %v ---- from ---- %v\n", i, s.RemoteAddr())
if err := soc.Emit("namespace:message -- "+fmt.Sprintf("%v ------> %v", i, s.RemoteAddr()), "message"); err != nil {
t.Error(err)
}
},
}
soc := nsocket.New(nsocket.Config{
AuthFunc: func(r *http.Request) (ok bool) { // Optional: Add authentication : On https Websocket send cookie to the server
c, err := r.Cookie("auth")
if err != nil {
return
}
return c.Value == authKey
},
AllowedOrigins: []string{"localhost:3000", "localhost:8000"}, //Optional: Set allowed origins if needed
Namespace: nsocket.Namespace{
nsocket.Default: events // Events functions are called when message is received from the client
},
})
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := soc.Serve(w, r); err != nil {
panic(err)
}
})
fmt.Println("starting server on: http://localhost:8000")
http.ListenAndServe(":8000", mux) // Start websocket server
```
### Note
This library is still under development and not production ready, use with caution.