8000 Server-side WebSocket upgrade (Net.HTTP.WebSocket is client-only) · Issue #345 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Server-side WebSocket upgrade (Net.HTTP.WebSocket is client-only) #345

Closed

Description

@akoskovacs

Net.HTTP.WebSocket (src/stdlib/net/http/websocket.kex) implements exactly one side of RFC 6455: WebSocket.connect(url) opens an outbound connection and returns a Connection with send, receiveMessage, session, close, closed?. There is no way to accept an incoming handshake — no upgrade response type, and no way for a Net.HTTP.Server route handler to take over the connection.

What's missing

  • Net.HTTP.Response only builds binary/text/empty — no upgrade/hijack response type.
  • The server-side intrinsic (runtime/src/kex_intrinsic_nethttpserver.erl) has no upgrade path at all.
  • kex_intrinsic_netwebsocket.erl is the client handshake exclusively (computes Sec-WebSocket-Key, validates Sec-WebSocket-Accept) — nothing generates a server accept response or does server-side frame masking/validation.

This isn't a surprise gap — docs/net-plan.md already designs it and lists it as missing:

| WebSocket | Partial | Verified ws/wss client handshake, subprotocols, masked sends, fragmented high-level receive, UTF-8/close/limit checks, automatic pong | Server upgrades, raw frames, heartbeat, reconnecting client, scripted mock, browser implementation |

with a sketch of the intended shape (docs/net-plan.md:670-687):

router.get("/socket") do |request, context|
  authenticate(request).map do |principal|
    WebSocket.upgrade(request) do |handshake|
      if handshake.subprotocols.contains?("chat.v2") then
        Accept({ |socket| serveChat(principal, socket) }, headers: Headers.empty, subprotocol: Just("chat.v2"))
      else
        Reject(Response.text(426, "chat.v2 required"))
      end
    end
  end
end

Why it matters

Found while scoping WebSocket route support for Rodolfo (kexhq/rodolfo), a small Sinatra/Kemal-inspired web framework — both of those frameworks support WebSocket routes, because their underlying servers (Rack's hijack API, Crystal's HTTP::Server upgrade) hand the framework the raw socket mid-request. Kex's Net.HTTP.Server has no equivalent hook, so a route handler can't take over the connection.

Net.Socket.TCP.listen/accept does give raw byte-level access, so a hand-rolled RFC 6455 server is technically reachable from application code today — but only as its own listener on a separate port, since there's no way to peek at a connection before Net.HTTP.Server claims it. Sharing one port with ordinary HTTP routes would mean reimplementing HTTP/1.1 request parsing from scratch, which isn't something a framework should have to do to get WebSocket support. Rodolfo route-level support (ws "/path" do |socket| ... end, mirroring get/post) is blocked on this landing in Kex.

Environment

Found against kex 0.4.0-beta.3 (293a0b5), both the version bundled at the time and a clean rebuild of origin/main at the same commit.

Activity

  1. added this to the 0.4.0 milestone on Sep 15, 2026
  2. added a commit that references this issue on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNew feature or requestImportantNeeds attention sooner rather than laterStdlib

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0