kemalcr/kemal/kemal-websocket
Claude Code and agent skill by kemalcr/kemal
- community
Checking that the SKILL.md file is still in the repository.
Implementing real-time bi-directional communication with WebSockets in Kemal, origin security, and lifecycle management.
Install
npx vskill@latest install kemalcr/kemal/kemal-websocketSKILL.md
The same SKILL.md was published earlier as kemalcr/kemal/kemal-websocket.
Kemal WebSocket Integration
This skill provides expert guidance on using WebSockets for real-time features in Kemal, with version-aware origin security guidance, strictly following patterns from `kemal-by-example/real-time-dashboard` and `kemal-by-example/twitter-clone`.
Version Notes
- Default Origin policy changed in 1.13.0: with an empty
websocket_allowed_origins(the default), Kemal 1.13.0+ enforces same-origin and rejects missing or mismatchedOriginheaders with403 Forbidden; releases up to 1.12.0 allow all origins (CSWSH risk — configure an allowlist explicitly, see below). websocket_allowed_origins = ["*"]opt-in allow-all: since Kemal 1.13.0.- Non-GET upgrade rejection (RFC 6455 §4.1,
405 Method Not Allowed+Allow: GET) and explicitConnection: closeafter a rejected handshake: since Kemal 1.13.0.
Core Mandates
- WebSocket Route Definition: Use the
wshelper to define WebSocket endpoints:
``crystal ws "/socket" do |socket, env| # Socket lifecycle callbacks end ``
- Origin Validation & CSWSH Security:
- CRITICAL ON STABLE RELEASES (1.12.0 and earlier): An empty
Kemal.config.websocket_allowed_origins = [] of String(default) permits all origins (return true if allowed.empty?).
To protect against Cross-Site WebSocket Hijacking (CSWSH) in production on these releases, you MUST explicitly configure allowed origins:
``crystal # MANDATORY on 1.12.0 and earlier to protect against CSWSH: Kemal.config.websocket_allowed_origins = %w[https://myapp.com http://localhost:3000] ``
- ON KEMAL 1.13.0+: An empty
websocket_allowed_originsdefaults to strict same-origin enforcement (matching requestHost). Missing or mismatchedOriginheaders are rejected with403 Forbidden. To explicitly allow all origins:
``crystal # Kemal 1.13.0+ — opt-in allow-all: Kemal.config.websocket_allowed_origins = ["*"] ``
- Protocol Compliance (RFC 6455 §4.1) — *[Kemal 1.13.0+]*:
- On Kemal 1.13.0+, WebSocket upgrade handshakes require the
GETmethod. Non-GET upgrade requests (POST,QUERY, etc.) are automatically rejected with405 Method Not Allowed,Allow: GET, andConnection: close. - Failed handshakes close the connection immediately to prevent WebSocket connection smuggling.
- Socket Lifecycle Callbacks:
socket.on_message: Handle incoming text messages from clientsocket.on_close: Handle socket disconnection and perform cleanupsocket.on_ping/socket.on_pong: Handle heartbeat signals
- Concurrency Safety: Always protect shared socket collections with a
Mutex:
```crystal class Hub @sockets = Array(HTTP::WebSocket).new @mutex = Mutex.new
def register(socket) @mutex.synchronize { @sockets << socket } end
def unregister(socket) @mutex.synchronize { @sockets.delete(socket) } end end ```
- Error Handling: Rescue
IO::Error | Socket::Errorwhen sending to sockets and unregister closed sockets.
Patterns from Source Code
Centralized WebSocket Hub (real-time-dashboard/src/services/dashboard_hub.cr)
WebSocket Route Setup with Origin Security
Best Practices
- Security First: Always configure
Kemal.config.websocket_allowed_originsexplicitly in production. Origin validation is a browser boundary, not authentication/authorization. - Mutex Synchronization: Wrap all modifications and iterations over socket lists in
@mutex.synchronize. - Selective Rejection: Use
@sockets.reject!inside broadcast loops to cleanly strip dead sockets without deadlocks. - Graceful Teardown: Unregister sockets in
on_closecallbacks.
When to Use
- When building real-time bi-directional chat applications, collaborative tools, or active telemetry dashboards.
- When client and server communicate via continuous socket connections.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryChecking.github/skills/kemal-websocket/SKILL.md
Popularity
- Repository stars
- 3,918
- Repository forks
- 205
- 7-day trend score
- 41
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/kemalcr/kemal/kemal-websocket)