https://github.com/olahol/melody
:notes: Minimalist websocket framework for Go
https://github.com/olahol/melody
example framework go golang minimalist websocket websocket-framework
Last synced: 7 months ago
JSON representation
:notes: Minimalist websocket framework for Go
- Host: GitHub
- URL: https://github.com/olahol/melody
- Owner: olahol
- License: bsd-2-clause
- Created: 2015-05-13T20:38:32.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-07-25T18:08:25.000Z (over 1 year ago)
- Last Synced: 2025-04-08T17:14:06.479Z (7 months ago)
- Topics: example, framework, go, golang, minimalist, websocket, websocket-framework
- Language: Go
- Homepage:
- Size: 987 KB
- Stars: 3,885
- Watchers: 64
- Forks: 371
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - melody - WebSocket 服务框架 (开源类库 / WebSocket)
- awesome-trevor - Melody - minimalist websocket framework for Go (Programming / Golang)
- go-awesome - melody - WebSocket service framework (Open source library / WebSocket)
- awesome-go - melody - Minimalist websocket framework for Go - ★ 1187 (Messaging)
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Advanced Console UIs)
- awesome-go-cn - melody
- awesome-go-cn - melody
- awesome-go-plus - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling.  (Messaging / Search and Analytic Databases)
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-go - olahol/melody
- awesome-go - melody - | - | - | (Messaging / Advanced Console UIs)
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. Stars:`4.0K`. (Messaging / Search and Analytic Databases)
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-golang-repositories - melody
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. - :arrow_down:34 - :star:478 (Messaging / Advanced Console UIs)
- awesome-go-cn - melody
- fucking-awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-gin - olahol/melody
- awesome-go-extra - melody - 05-13T20:38:32Z|2022-03-09T11:29:28Z| (Messaging / Advanced Console UIs)
- awesome-go-zh - melody
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (<span id="消息-messaging">消息 Messaging</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go-with-stars - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-Char - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Advanced Console UIs)
- zero-alloc-awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Search and Analytic Databases)
- awesome-go-cn - melody
- awesome-go - melody - Minimalist framework for dealing with websocket sessions, includes broadcasting and automatic ping/pong handling. (Messaging / Advanced Console UIs)
README
# melody

[](https://app.codecov.io/github/olahol/melody)
[](https://goreportcard.com/report/github.com/olahol/melody)
[](https://godoc.org/github.com/olahol/melody)
> :notes: Minimalist websocket framework for Go.
Melody is websocket framework based on [github.com/gorilla/websocket](https://github.com/gorilla/websocket)
that abstracts away the tedious parts of handling websockets. It gets out of
your way so you can write real-time apps. Features include:
* [x] Clear and easy interface similar to `net/http` or Gin.
* [x] A simple way to broadcast to all or selected connected sessions.
* [x] Message buffers making concurrent writing safe.
* [x] Automatic handling of sending ping/pong heartbeats that timeout broken sessions.
* [x] Store data on sessions.
## Install
```bash
go get github.com/olahol/melody
```
## [Example: chat](https://github.com/olahol/melody/tree/master/examples/chat)
[](https://github.com/olahol/melody/tree/master/examples/chat)
```go
package main
import (
"net/http"
"github.com/olahol/melody"
)
func main() {
m := melody.New()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
m.HandleRequest(w, r)
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
m.Broadcast(msg)
})
http.ListenAndServe(":5000", nil)
}
```
## [Example: gophers](https://github.com/olahol/melody/tree/master/examples/gophers)
[](https://github.com/olahol/melody/tree/master/examples/gophers)
```go
package main
import (
"fmt"
"net/http"
"sync/atomic"
"github.com/olahol/melody"
)
var idCounter atomic.Int64
func main() {
m := melody.New()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
m.HandleRequest(w, r)
})
m.HandleConnect(func(s *melody.Session) {
id := idCounter.Add(1)
s.Set("id", id)
s.Write([]byte(fmt.Sprintf("iam %d", id)))
})
m.HandleDisconnect(func(s *melody.Session) {
if id, ok := s.Get("id"); ok {
m.BroadcastOthers([]byte(fmt.Sprintf("dis %d", id)), s)
}
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
if id, ok := s.Get("id"); ok {
m.BroadcastOthers([]byte(fmt.Sprintf("set %d %s", id, msg)), s)
}
})
http.ListenAndServe(":5000", nil)
}
```
### [More examples](https://github.com/olahol/melody/tree/master/examples)
## [Documentation](https://godoc.org/github.com/olahol/melody)
## Contributors
## FAQ
If you are getting a `403` when trying to connect to your websocket you can [change allow all origin hosts](http://godoc.org/github.com/gorilla/websocket#hdr-Origin_Considerations):
```go
m := melody.New()
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
```