MCPcopy
hub / github.com/olahol/melody

github.com/olahol/melody @v1.4.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.4.0 ↗
110 symbols 547 edges 14 files 46 documented · 42%
README

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:

  • [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

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 }

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Close
called by 64
melody.go
closed
called by 14
hub.go
HandleConnect
called by 14
melody.go
HandleMessage
called by 14
melody.go
Write
called by 13
session.go
writeMessage
called by 9
session.go
closed
called by 9
session.go
Get
called by 9
session.go

Shape

Method 57
Function 42
Struct 6
FuncType 5

Languages

Go100%

Modules by API surface

melody.go35 symbols
melody_test.go34 symbols
session.go23 symbols
hub.go9 symbols
config.go2 symbols
examples/multichat/main.go1 symbols
examples/gophers/main.go1 symbols
examples/filewatch/main.go1 symbols
examples/chat/main.go1 symbols
examples/chat-gin/main.go1 symbols
examples/chat-echo/main.go1 symbols
envelope.go1 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

github.com/bytedance/sonicv1.9.1 · 1×
github.com/chenzhuoyu/base64xv0.0.0-2022111506244 · 1×
github.com/davecgh/go-spewv1.1.1 · 1×
github.com/fsnotify/fsnotifyv1.5.4 · 1×
github.com/gabriel-vasile/mimetypev1.4.2 · 1×
github.com/gin-contrib/ssev0.1.0 · 1×
github.com/go-playground/localesv0.14.1 · 1×
github.com/go-playground/universal-translatorv0.18.1 · 1×
github.com/goccy/go-jsonv0.10.2 · 1×
github.com/golang-jwt/jwtv3.2.2+incompatible · 1×

For agents

$ claude mcp add melody \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact