nitrojs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.build
MIT License
6.29k stars 519 forks source link

WebSocket Support Tracker #2171

Open pi0 opened 9 months ago

pi0 commented 9 months ago

Initial PR: https://github.com/unjs/nitro/pull/2170

Relavant docs:

Platform support

Needs investigation:

passionate-bram commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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:  `pnpm@8.15.4`
- Builder:          `-`
- User Config:      `nitro`
- Runtime Modules:  `-`
- Build Modules:    `-`
pi0 commented 8 months ago

@StringKe You need to use nuxi-nightly.

daniandl commented 8 months ago

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 commented 8 months ago

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

StringKe commented 8 months ago

does crossws have an API to proactively close connection?

indam1 commented 8 months ago

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

FliPPeDround commented 7 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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.

serhalp commented 3 months ago

👋🏼 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 commented 3 months ago

@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 commented 3 months ago

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

adamdehaven commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 1 month ago

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 commented 3 weeks ago

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

tahirmahmudzade commented 2 weeks ago

@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 ?