henriqueatila/golang-gin-best-practices / skills/golang-gin-api
golang-gin-api
VerifiedBuild REST APIs with Go Gin. Use when creating Go web servers, adding Gin routes, writing handlers, or asking about middleware, binding, error handling, or project structure.
references/websocket-chat-handler.md
View on GitHub# WebSocket — ChatHandler (Hub + Client Registration)
See also: `websocket-hub-and-client.md`, `websocket-setup-and-echo.md`
## ChatHandler
Upgrades the HTTP connection and registers the new `Client` with the `Hub`. Launches `readPump` and `writePump` goroutines.
```go
// internal/handler/ws_chat_handler.go
package handler
import (
"log/slog"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"myapp/pkg/ws"
)
// ChatHandler upgrades the connection and registers the client with the hub.
func ChatHandler(hub *ws.Hub) gin.HandlerFunc {
return func(c *gin.Context) {
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
slog.Error("ws upgrade failed", "err", err)
return
}
// c.Copy() is required when passing *gin.Context to goroutines.
// Capture request-scoped values before the handler returns.
cpy := c.Copy()
_ = cpy // use cpy.GetString("userID") etc. if needed
client := &ws.Client{
Hub: hub,
Conn: conn,
Send: make(chan []byte, 256),
}
hub.Register <- client
go client.WritePump()
go client.ReadPump()
}
}
```
**Why `c.Copy()` before goroutines:** Gin recycles `*gin.Context` objects via `sync.Pool` after the handler returns. `c.Copy()` creates a snapshot safe to use after the handler returns.
## Route Registration
```go
hub := ws.NewHub()
go hub.Run()
r.GET("/ws/chat", middleware.Auth(cfg, logger), handler.ChatHandler(hub))
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |