olahol / melody

:notes: Minimalist websocket framework for Go
BSD 2-Clause "Simplified" License
3.75k stars 366 forks source link
example framework go golang minimalist websocket websocket-framework

melody

Build Status Codecov Go Report Card GoDoc

:notes: Minimalist websocket framework for Go.

Melody is websocket framework based on 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:

Install

go get github.com/olahol/melody

Example: chat

Chat

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

Gophers

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

Documentation

Contributors

FAQ

If you are getting a 403 when trying to connect to your websocket you can change allow all origin hosts:

m := melody.New()
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }