You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ethers WebSocket providers (such as the one created by ethers.providers.InfuraProvider.getWebSocketProvider(...)) do not work in the Cloudflare Workers environment. They throw a Failed to construct 'WebSocket': the constructor is not implemented error because Cloudflare Workers have their own preferred way of creating a socket connection and do not support the vanilla WebSocket constructor.
from within any Cloudflare Worker. This is a bit of an annoying one to setup if you've not already got a Cloudflare account and Wrangler installed, but it's an immediate thing to replicate. Either of the scenarios above will throw the aforementioned Failed to construct 'WebSocket': the constructor is not implemented error.
Environment
Workers is Cloudflare's offering for serverless compute, similar to other offerings like AWS Lambda, Azure Functions, GCP Cloud Functions, Vercel Functions, Netlify Functions, or Serverless Cloud. Unlike any of those other environments, Cloudflare Workers do not use a Node runtime. Instead, Cloudflare Workers execute directly on v8 isolates with some custom Cloudflare implementations of standard JavaScript APIs. Normally, this means that any code which works on the browser will work on a Cloudflare Worker.
However, whether due to security, performance, or time constraints, Cloudflare's implementation does not have complete coverage of the JavaScript APIs. In particular the lack of WebSocket API constructor is what makes the WebSocket providers unusable.
Search Terms
This is similar to #1886 as reported by @wighawag in that a Cloudflare difference to browser JavaScript was responsible for both.
I believe the most straight-forward solution to this issue is supporting a way to pass an existing WebSocket along to the WebSocketProvider instead of always constructing a new WebSocket. Is this something you would be open to PRs for, @ricmoo?
The text was updated successfully, but these errors were encountered:
Description
Ethers WebSocket providers (such as the one created by
ethers.providers.InfuraProvider.getWebSocketProvider(...)
) do not work in the Cloudflare Workers environment. They throw aFailed to construct 'WebSocket': the constructor is not implemented
error because Cloudflare Workers have their own preferred way of creating a socket connection and do not support the vanilla WebSocket constructor.Reproduction
Attempt to call either
or
from within any Cloudflare Worker. This is a bit of an annoying one to setup if you've not already got a Cloudflare account and Wrangler installed, but it's an immediate thing to replicate. Either of the scenarios above will throw the aforementioned
Failed to construct 'WebSocket': the constructor is not implemented
error.Environment
Workers is Cloudflare's offering for serverless compute, similar to other offerings like AWS Lambda, Azure Functions, GCP Cloud Functions, Vercel Functions, Netlify Functions, or Serverless Cloud. Unlike any of those other environments, Cloudflare Workers do not use a Node runtime. Instead, Cloudflare Workers execute directly on v8 isolates with some custom Cloudflare implementations of standard JavaScript APIs. Normally, this means that any code which works on the browser will work on a Cloudflare Worker.
However, whether due to security, performance, or time constraints, Cloudflare's implementation does not have complete coverage of the JavaScript APIs. In particular the lack of WebSocket API constructor is what makes the WebSocket providers unusable.
Search Terms
This is similar to #1886 as reported by @wighawag in that a Cloudflare difference to browser JavaScript was responsible for both.
Discussion
A special thanks to @mrbbot of Miniflare fame for helping me identify this issue in the Cloudflare Workers Discord.
I believe the most straight-forward solution to this issue is supporting a way to pass an existing WebSocket along to the
WebSocketProvider
instead of always constructing a new WebSocket. Is this something you would be open to PRs for, @ricmoo?The text was updated successfully, but these errors were encountered: