edge-runtime icon indicating copy to clipboard operation
edge-runtime copied to clipboard

WebSocket client drops some incoming messages when running locally

Open rnbrady opened this issue 1 year ago • 3 comments

Bug Report

A WebSocket client instantiated in the Edge runtime will randomly drop incoming messages from the WedSocket server. In my test every 10th or so incoming message is dropped, but it probably depends on timing and message length (edit: it's actually triggered by a websocket keepalive).

Possible solution

This appears to be caused by by upstream bug nodejs/undici#2859 which has been fixed and released in nodejs/[email protected].

Bumping Undici from 5 to 6 appears to be a breaking change so Dependabot hasn't been able to do it automatically. Possibily complicating matters further is that Undici v6 does not support Node v16.

Additional context/screenshots

In my application I'm connecting from a server-side Edge function to a Bitcoin indexer over WebSocket to subscribe to balance updates which I then send to the client as server-sent events (SSE). After an initial handshake I ping the indexer over WebSocket every second, and the pongs are received except that every 10th or so one gets dropped and a null message is delivered:

Screenshot 2024-10-04 at 16 20 26

With OpenAI recently annoucing their Realtime API based on WebSockets, other developers might soon run into this problem too.

tl;dr: please try and merge #947 🙏

rnbrady avatar Oct 04 '24 15:10 rnbrady

On further investigation the problem occurs when the server sends a websocket ping message. See rnbrady/vercel-edge-runtime-983 to reproduce.

Screenshot 2024-10-05 at 23 13 22

rnbrady avatar Oct 06 '24 06:10 rnbrady

I am unable to reproduce this bug in deployments to Vercel.

To aid in testing I have deployed the server.mjs echo server which sends a ping every 3s to the following public address: https://vercel-edge-runtime-983-ws-server-1014083751163.europe-west2.run.app

rnbrady avatar Oct 06 '24 19:10 rnbrady

This might be resolved by #546.

rnbrady avatar Oct 22 '24 20:10 rnbrady

Hello, this is going to be tricky to resolve since edge-runtime still supports Node.js v16, and to make this possible we are sticky to undici v5:

https://github.com/vercel/edge-runtime/blob/main/packages/primitives/package.json#L33

The fix for the undici websocket version was shipped under undici v6, which is dropping Node.js v16 support.

So in order to ship this we need to drop Node.js v16 support and upgrade (or drop) Undici , which is covered in https://github.com/vercel/edge-runtime/issues/546

Kikobeats avatar Nov 21 '24 14:11 Kikobeats

Thanks for the reply @Kikobeats. I'm happy to wait for #546.

I just looked into how Cloudflare Workers does WebSocket in local dev and it turns out they have problems too, so I appreciate it's not an easy probem to solve.

rnbrady avatar Nov 21 '24 21:11 rnbrady

Edge Runtime v4 has been, dropping node18 support and it's now using undici@6.

This should be fix your issue @rnbrady as soon as soon as the new version starts being used in Next.js: https://github.com/vercel/next.js/pull/73432

Kikobeats avatar Dec 02 '24 15:12 Kikobeats

Thanks @Kikobeats, I will try it out once it's merged and let you know. Appreciate your work on this. Dev prod disparity in Edge functions has taken me on quite a ride lately, so this will be a nice fix to have. Appreciate your work.

rnbrady avatar Dec 02 '24 16:12 rnbrady

It's shipped starting from Next.js v15.0.4-canary.37

Closing but please tell me if it's working fine for you now 🙂

Kikobeats avatar Dec 04 '24 15:12 Kikobeats

@Kikobeats I can confirm this is fixed. Tested against 15.0.4-canary.41. Thank you so much. This will allow me to significantly simplify my dev environment.

rnbrady avatar Dec 05 '24 11:12 rnbrady