WebSocket usage with worktop
See original GitHub issueI’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:
- Created 2 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey! 👋
miniflare@2.0.0-rc.3
has just been released, including the fix for this. You can find the changelog here.Yeah, miniflare’s
Response
class has a private#webSocket
property – it’s a publicreadonly
in the runtime.And here’s the part where our
Response
is only looking for a.webSocket
property on aResponseInit
value: https://github.com/cloudflare/miniflare/blob/v2/packages/core/src/standards/http.ts#L377-L396