Effect CommunityEC
Effect Community2y ago
38 replies
DwieDave

[SOLVED] Effect HTTP Server Websocket Upgrade

Hey everyone 🙂
I am trying to get a combined HTTP Server (using the Effect HTTP Server from Platform) and Websocket server that uses both the same Node HTTP Server port.
Very similar to @whatplan s workshop project (https://github.com/ethanniser/effect-workshop/blob/0e1d4b7c887fd5c515d332421648271976509ea1/src/part3-webserver/breakpoints/99-effect-client/server/ws.ts#L230)
Which is a good project to reproduce my issue:
When installing and running the server with bun it works out of the box and I can connect Postman via WS and send messages.

When running with tsx or ts-node im getting the following Error message in Postman:
Error: Invalid WebSocket frame: RSV1 must be clear

The error vanishes when the linked WebsocketServer is created with option {port: 3001} instead of the node.js HTTP server passed in directly as seen in the workshop files.
It also vanishes when I remove the HTTPLive Router implementation from the merged layer.

For me it feels like that on node the Effect HTTP Server is not behaving the same way as on bun although the NodeRuntime is used and not BunRuntime.

I've tried to manually upgrade the Request in the Effect HTTP Router like this:
export const HTTPRouterLive = C.API_PREFIX.pipe(
  E.flatMap((prefix) =>
    Http.router.empty.pipe(
      //TODO: add prefixed routes above
      Http.router.prefixAll(`/${prefix}`),
      Http.router.get("/",
        Http.request.ServerRequest.pipe(
          E.flatMap((req) => req.upgrade),
          E.as(Http.response.empty())
        )
      ),
      Http.router.get(
        "/healthz",
        E.gen(function*(_) {
          return yield* _(Http.response.json({ status: "ok" }));
        }),
      ),
    ),
  ),
  Http.server.serve(Http.middleware.logger),
);

But this yields a different error: Error: Unexpected server response: 204 (also when providing status: 101) it is the same error but with the different status code.
GitHub
Contribute to ethanniser/effect-workshop development by creating an account on GitHub.
effect-workshop/src/part3-webserver/breakpoints/99-effect-client/se...
Was this page helpful?