Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket Support Tracker #2171

Open
11 of 27 tasks
pi0 opened this issue Feb 25, 2024 · 22 comments
Open
11 of 27 tasks

WebSocket Support Tracker #2171

pi0 opened this issue Feb 25, 2024 · 22 comments

Comments

@pi0
Copy link
Member

pi0 commented Feb 25, 2024

Initial PR: #2170

Relavant docs:

Platform support

  • Dev server
    • Runtime handlers
    • Dev handlers
  • bun
  • Cloudflare
    • cloudflare-pages
    • cloudflare-module
    • cloudflare (worker)
  • Deno
    • deno-deploy
    • deno-server
  • Node
    • node-server
    • node via websocket export
    • node-cluster

Needs investigation:

  • aws-lambda
  • AWS amplify
  • Firebase
  • Netlify
    • netlify-edge
    • netlify-lambda
  • stormkit
  • Vercel
    • vercel-lambda
    • vercel-edge
  • wnterjs
  • zerabur
@passionate-bram
Copy link
Contributor

After reviewing the Netlify documentation, I found no suitable offering that will properly support websocket servers at this time.

  1. Netlify Functions are killed off after 10 seconds. This limit can be increased to 26 seconds if you contact support.
  2. Netlify Edge-Functions have max 10 seconds of CPU time.
  3. Netlify Background Functions can run for 15 minutes, but they cannot send a response to the request that triggered them.

@pi0
Copy link
Member Author

pi0 commented Mar 4, 2024

Thanks for your investigation. Re Netlify, i have reached out and waiting for an answer if there is any viable current or future plans.

@StringKe
Copy link

StringKe commented Mar 15, 2024

Is websocket still not available in the current development mode of the latest nuxt?

I try to access /websocket in development mode (pnpm run dev) and it's always connecting.

If I compile and start with node .output/server/index.mjs everything works fine.

https://github.com/StringKe/report-nuxt-webscoket

- Operating System: `Darwin`
- Node Version:     `v21.5.0`
- Nuxt Version:     `3.10.3`
- CLI Version:      `3.10.1`
- Nitro Version:    `2.9.3`
- Package Manager:  `[email protected]`
- Builder:          `-`
- User Config:      `nitro`
- Runtime Modules:  `-`
- Build Modules:    `-`

@pi0
Copy link
Member Author

pi0 commented Mar 15, 2024

@StringKe You need to use nuxi-nightly.

@daniandl
Copy link

Are there plans to let us define which adapters defineWebsocketHandler uses internally?
Specifically, in my case I want to use the crossws uWs adapter

https://github.com/unjs/nitro/pull/2170/files#diff-874de70a2b8dc27aa6a857ee5110f1b1ab675afc9e6b1191bebfb1119ad7c78fR7

Or is it possible to just import it manually and use it instead of defineWebsocketHandler?

@pi0
Copy link
Member Author

pi0 commented Mar 18, 2024

uws requires it's own server preset for Nitro to be supported. (because it is not based on node:http)

@StringKe
Copy link

does crossws have an API to proactively close connection?

@indam1
Copy link

indam1 commented Mar 29, 2024

Does nuxt support websocket when deployed on cloudflare pages? Everything is fine on localhost, but on Cloudflare I'm getting only status finished

@FliPPeDround
Copy link

FliPPeDround commented Apr 2, 2024

Hello, I am trying to combine peer-server with my Nuxt application. Unfortunately, it doesn't work as expected.
The reproduced issue is in nuxt/nuxt

In the peer-server middleware, ws is used to send the WebSocket message
https://github.com/peers/peerjs-server/blob/master/src/services/webSocketServer/index.ts#L59

import { WebSocketServer as Server } from "ws";

const options: WebSocket.ServerOptions = {
	path: this.path,
	server,
};

this.socketServer = new Server(options);

this.socketServer.on("connection", (socket, req) => {
	this._onSocketConnection(socket, req);
});

Currently, Nuxt is unable to listen for web socket connection events as expected

@meesvrh
Copy link

meesvrh commented Jun 30, 2024

Does nuxt support websocket when deployed on cloudflare pages? Everything is fine on localhost, but on Cloudflare I'm getting only status finished

Hi did u manage to get this fixed?

@pileroniel
Copy link

Thanks for your investigation. Re Netlify, i have reached out and waiting for an answer if there is any viable current or future plans.

Hi, @pi0 any updates on this from the netlify team?

@pi0
Copy link
Member Author

pi0 commented Jul 2, 2024

For netlify, you might use SSE (long polling) as much as know. @serhalp might give better answer if there is any plans for future WebSocket API.

david-mears-2 added a commit to jameel-institute/daedalus-web-app that referenced this issue Jul 8, 2024
This should allow multi-core handling.
https://nitro.unjs.io/deploy/runtimes/node

It is not considered compatible with websockets.
nitrojs/nitro#2171
david-mears-2 added a commit to jameel-institute/daedalus-web-app that referenced this issue Jul 8, 2024
Best test is if we build and then run 'npm run preview' to get
production-like output.

Test is failed: websockets not compatible with node-cluster mode, as expected
by nitrojs/nitro#2171
@serhalp
Copy link
Contributor

serhalp commented Jul 29, 2024

👋🏼 I'm not aware of any plans to change Netlify's support for WebSockets. However, Server-Sent Events (SSE) work great with Netlify Edge Functions.

@adamdehaven
Copy link
Member

adamdehaven commented Aug 5, 2024

@pi0 is there an example somewhere that instructs how to set up websockets with the cloudflare-module Nitro preset?

When using the example from the docs, if I run locally in Nuxt (via nuxt dev) the tabs communicate properly; however, when I build and then run wrangler dev there is no communication between separate tabs.

I can see each peer is available (the number is incremented for each connected client if I output on the page) but I can not receive messages between them after calling subscribe/publish accordingly.

I see the following note on the CrossWS Pub / Sub page:

Native pub/sub is currently only available for Bun, Node.js (uWebSockets) and Cloudflare (durable objects)

but there are no instructions as to how to actually hook it up (here in the Nitro docs) to Cloudflare Durable Objects?

If you can point me in the right direction I can also push up a PR for docs.


Alternatively, if you have another (non-experimental) suggestion on how to implement similar messaging, I'd be open to try.

@pi0
Copy link
Member Author

pi0 commented Aug 6, 2024

Durable-ojbect support is on the progress (sorry for misleading docs, that section is not released yet)

@adamdehaven
Copy link
Member

Durable-ojbect support is on the progress (sorry for misleading docs, that section is not released yet)

😅 Well I feel better that I couldn't get it working now. Tag me if I can help test/implement/document

@bpbastos
Copy link

Hello, is there a way to access the event similar to defineEventHandler or eventHandler in Nuxt? I'm asking because I'm trying to retrieve a router parameter, and all server utility functions require the event parameter.

@Barbapapazes
Copy link
Member

Hello, is there a way to access the event similar to defineEventHandler or eventHandler in Nuxt? I'm asking because I'm trying to retrieve a router parameter, and all server utility functions require the event parameter.

A route parameter from the websocket route? what's your use case?

@bpbastos
Copy link

I'm trying to create a proxy for Parse Server's LiveQueries and need to pass parameters to initialize the SDK and perform the necessary queries. I'm not sure if this is the best approach, as I'm just starting out with WebSockets.

@sameignatovich
Copy link

sameignatovich commented Oct 19, 2024

Yeah, if it possible, please: add possibility to read params from url. It's might be useful to authenticate user with one-time/signed token.

Possible solution may be second (or first, ws still experimental feature anyway) argument to open() hook

UPD:

it's possible to parse params from peer.url string:

export default defineWebSocketHandler({
  open(peer) {
    console.log(peer.url); // `/some/usr/TOKEN/ws`
  },
});

@AgileInteractive
Copy link

What about platforms not mentioned, (e.g. Azure) are they omitted for any reason?

@tahirmahmudzade
Copy link

@pi0 is there an example somewhere that instructs how to set up websockets with the cloudflare-module Nitro preset?

When using the example from the docs, if I run locally in Nuxt (via nuxt dev) the tabs communicate properly; however, when I build and then run wrangler dev there is no communication between separate tabs.

I can see each peer is available (the number is incremented for each connected client if I output on the page) but I can not receive messages between them after calling subscribe/publish accordingly.

I see the following note on the CrossWS Pub / Sub page:

Native pub/sub is currently only available for Bun, Node.js (uWebSockets) and Cloudflare (durable objects)

but there are no instructions as to how to actually hook it up (here in the Nitro docs) to Cloudflare Durable Objects?

If you can point me in the right direction I can also push up a PR for docs.

Alternatively, if you have another (non-experimental) suggestion on how to implement similar messaging, I'd be open to try.

hey, currently i'm also facing the same issue, connections works pretty well in local dev nuxt dev. However can't see incoming data when running with wrangler dev. Any lead on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests