question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket usage with worktop

See original GitHub issue

I’m using worktop@0.8.0-next.8 with miniflare@2.0.0-rc.. I got this error when trying to connect to ws route. Could you take a look? I understand that I’m using unstable releases.

GET /ws 200 OK (1.74ms)
[mf:err] TypeError: Web Socket request did not return status 101 Switching Protocols response with Web Socket

This is index.js.

import { Router } from 'worktop'
import { listen } from 'worktop/ws'
import { reply } from 'worktop/module'

const API = new Router()

API.add(
  `GET`,
  `/ws`,
  listen((req, ctx, socket) => {
    console.log('connected')
  })
)

export default reply(API.run)

Same error when not using worktop/ws.

import { Router } from 'worktop'
import { reply } from 'worktop/module'

const API = new Router()

API.add(`GET`, `/ws`, async (req, ctx) => {
  const upgradeHeader = req.headers.get(`Upgrade`)

  if (upgradeHeader !== `websocket`) {
    return new Response(`Expected websocket`, { status: 400 })
  }

  const [client, server] = Object.values(new WebSocketPair())

  server.accept()

  return new Response(null, {
    status: 101,
    webSocket: client,
  })
})

export default reply(API.run)

The minimal ws example works fine.


async function handleRequest(request) {
  try {
    const url = new URL(request.url)
    switch (url.pathname) {
      case `/`:
        return new Response(`hello`)

      case `/ws`:
        const upgradeHeader = request.headers.get(`Upgrade`)

        if (upgradeHeader !== `websocket`) {
          return new Response(`Expected websocket`, { status: 400 })
        }

        const [client, server] = Object.values(new WebSocketPair())
        server.accept()

        return new Response(null, {
          status: 101,
          webSocket: client,
        })

      default:
        return new Response(`Not found`, { status: 404 })
    }
  } catch (err) {
    return new Response(err.toString())
  }
}

export default {
  async fetch(request: Request, env) {
    try {
      return await handleRequest(request, env)
    } catch (e) {
      return new Response(`${e}`)
    }
  },
}

I’m also using esbuild with below config, if that is useful.

import { build } from 'esbuild'

build({
  entryPoints: [`src/index.js`],
  outfile: `dist/index.mjs`,

  bundle: true,
  charset: `utf8`,
  format: `esm`,
  mainFields: [`browser`, `module`, `main`],
  platform: `neutral`,
  target: `es2020`,

  sourcemap: true,
  minify: true,
}).catch((err) => {
  console.error(err.stack)
  process.exitCode = 1
})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mrbbotcommented, Dec 8, 2021

Hey! 👋 miniflare@2.0.0-rc.3 has just been released, including the fix for this. You can find the changelog here.

1reaction
lukeedcommented, Nov 29, 2021

Yeah, miniflare’s Response class has a private #webSocket property – it’s a public readonly in the runtime.

And here’s the part where our Response is only looking for a .webSocket property on a ResponseInit value: https://github.com/cloudflare/miniflare/blob/v2/packages/core/src/standards/http.ts#L377-L396

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Are WebSockets, and When Should You Use Them?
WebSockets are just one tool that fits into a larger arsenal when developing real-time, communication based applications. It is possible to build off...
Read more >
Writing WebSocket client applications - Web APIs | MDN
WebSocket client applications use the WebSocket API to communicate with WebSocket servers using the WebSocket protocol.
Read more >
how to replace socket covers
How To Replace Socket CoversAs the sockets tend to start closing after 48 hours. Carefully remove the battery and replace …. Yamaha FJR1300...
Read more >
What is WebSocket and How It Works? ⚙️ - Wallarm
The most customary use of WebSocket is in real-time application development wherein it assists in a continual display of data at the client...
Read more >
Using WebSockets · Cloudflare Workers docs
WebSockets are open connections sustained between the client and the origin server. Inside a WebSocket connection, the client and the origin can ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found