agentleFS
jkanindajkaninda/okapi-skills/websocket/SKILL.md

websocket

Skill3 starsChanged 3 months agoMITView on GitHub

WebSocket support ships as a separate, framework-agnostic package. It works with Okapi handlers and with plain net/http. The repository is jkaninda/okapi-ws, but the module path is github.com/jkaninda/okapiws — use that in go get and in imports: Built on github.com/gorilla/websocket. c.Response() implements Hijack, so the upgrader works directly: Or upgrade with defaults in one call: okapiws.Default(c.Response(), c.Request(), nil). *WSConnection: WSMessage: Type int, Data []byte, Event string, Error error.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Always defer ws.Close() after a successful upgrade.

    line 198
  2. Very specific:

    Keep per-connection state in your own struct — *okapi.Context is per-request, not per-connection.

    line 200

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Okapi WebSocket (okapiws)

WebSocket support ships as a separate, framework-agnostic package. It works with Okapi handlers and with plain net/http.

Installation

The repository is jkaninda/okapi-ws, but the module path is github.com/jkaninda/okapiws — use that in go get and in imports:

go get github.com/jkaninda/okapiws
import okapiws "github.com/jkaninda/okapiws"

Built on github.com/gorilla/websocket.

Upgrading Inside an Okapi Handler

c.Response() implements Hijack, so the upgrader works directly:

func WebSocket(config *okapiws.WSConfig, c *okapi.Context) (*okapiws.WSConnection, error) {
    upgrader := okapiws.NewWSUpgrader(config) // nil config = defaults
    return upgrader.Upgrade(c.Response(), c.Request(), nil)
}

func WebSocketWithHeaders(config *okapiws.WSConfig, headers http.Header, c *okapi.Context) (*okapiws.WSConnection, error) {
    upgrader := okapiws.NewWSUpgrader(config)
    return upgrader.Upgrade(c.Response(), c.Request(), headers)
}

Or upgrade with defaults in one call: okapiws.Default(c.Response(), c.Request(), nil).

Echo Server

func main() {
    app := okapi.Default()

    app.Get("/", func(c *okapi.Context) error {
        return c.OK(okapi.M{"message": "Hello from Okapi!"})
    })

    app.Get("/ws", handleWebSocket)

    if err := app.Start(); err != nil {
        panic(err)
    }
}

func handleWebSocket(c *okapi.Context) error {
    ws, err := WebSocket(nil, c)
    if err != nil {
        return err
    }
    defer func() {
        if err := ws.Close(); err != nil {
            log.Printf("error closing WebSocket: %v", err)
        }
    }()

    ws.OnMessage(func(msg *okapiws.WSMessage) {
        log.Printf("[%d] %s", msg.Type, msg.Data)
        _ = ws.Send(msg.Data) // echo back
    })

    ws.OnError(func(err error) { log.Printf("WebSocket error: %v", err) })
    ws.OnClose(func() { log.Println("client disconnected") })

    ws.Start()            // start the read/write pumps

    <-ws.Context().Done() // block until the connection closes
    return nil
}

With Plain net/http

http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
    upgrader := okapiws.NewWSUpgrader(nil)
    ws, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        http.Error(w, "WebSocket upgrade failed", http.StatusBadRequest)
        return
    }
    defer ws.Close()

    ws.OnMessage(func(msg *okapiws.WSMessage) { _ = ws.Send(msg.Data) })
    ws.OnError(func(err error) { log.Printf("WebSocket error: %v", err) })
    ws.Start()

    <-ws.Context().Done()
})

log.Fatal(http.ListenAndServe(":8080", nil))

Server API

okapiws.NewWSUpgrader(config *WSConfig) *WSUpgrader
upgrader.Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*WSConnection, error)
okapiws.Default(w, r, responseHeader) (*WSConnection, error)  // upgrade with default settings
okapiws.DefaultWSConfig() *WSConfig

*WSConnection:

ws.OnMessage(func(*okapiws.WSMessage))  // message callback
ws.OnError(func(error))                 // error callback
ws.OnClose(func())                      // close callback
ws.Send(data []byte) error              // text frame (non-blocking)
ws.SendText(text string) error
ws.SendBinary(data []byte) error
ws.SendJSON(v any) error
ws.SendEvent(event string, data any) error // {event, data} envelope
ws.Start()                              // start read/write goroutines
ws.Close() error                        // graceful close
ws.IsClosed() bool
ws.Context() context.Context            // cancelled when the connection closes

WSMessage: Type int, Data []byte, Event string, Error error.

WSConfig

FieldDefault
ReadBufferSize / WriteBufferSize1024
HandshakeTimeout10s
CheckOrigin func(*http.Request) boolfunc(*http.Request) bool { return true } — allows any origin; override in production
Subprotocolsnone
EnableCompressionfalse
PingInterval54s
PongWait60s
WriteWait10s
MaxMessageSize512 KB
cfg := &okapiws.WSConfig{
    CheckOrigin:    func(r *http.Request) bool { return r.Header.Get("Origin") == "https://app.example.com" },
    PingInterval:   30 * time.Second,
    PongWait:       40 * time.Second,
    MaxMessageSize: 1 << 20,
}
ws, err := okapiws.NewWSUpgrader(cfg).Upgrade(c.Response(), c.Request(), nil)

Ping/pong keep-alive is handled for you from PingInterval / PongWait.

Client

The package also ships a client with optional auto-reconnect:

client := okapiws.NewWSClient("wss://api.example.com/ws",
    okapiws.WithConfig(&okapiws.WSClientConfig{
        AutoReconnect:    true,
        ReconnectInitial: time.Second,
        ReconnectMax:     30 * time.Second,
        MaxRetries:       0, // unlimited
        Headers:          http.Header{"Authorization": {"Bearer " + token}},
    }))

client.OnConnect(func() { log.Println("connected") })       // fires on connect and each reconnect
client.OnMessage(func(msg *okapiws.WSMessage) { log.Printf("%s", msg.Data) })
client.OnError(func(err error) { log.Println(err) })
client.OnClose(func() { log.Println("closed") })

if err := client.Connect(ctx); err != nil {
    return err
}
defer client.Close()

_ = client.SendJSON(map[string]any{"type": "subscribe", "topic": "prices"})
<-client.Context().Done()

WSClientConfig adds TLSConfig, Subprotocols, EnableCompression, and the reconnect knobs to the shared buffer/timeout fields. okapiws.DefaultWSClient() returns the defaults.

Detecting an Upgrade Request

if c.IsWebSocketUpgrade() {
    // Connection: Upgrade + Upgrade: websocket present
}

okapi.LoggerMiddleware skips WebSocket upgrades, so a long-lived connection does not sit in the access log.

Tips

  • Always defer ws.Close() after a successful upgrade.
  • Block on <-ws.Context().Done() to keep the handler alive for the connection's lifetime.
  • Keep per-connection state in your own struct — *okapi.Context is per-request, not per-connection.
  • Set CheckOrigin explicitly — the default accepts every origin, which is fine for development only.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.