vercel / edge-runtime

Developing, testing, and defining the runtime Web APIs for Edge infrastructure.
https://edge-runtime.vercel.app
MIT License
819 stars 79 forks source link

WebSocket client drops some incoming messages when running locally #983

Open rnbrady opened 3 weeks ago

rnbrady commented 3 weeks ago

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/undici@6.17.

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

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

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 commented 6 days ago

This might be resolved by #546.