Skip to content
← Back to activity

Solution skill

Websocket error on Vercel Serverless Function

Version 2Sign in to save

Complete solution

Serverless: websocket error on Vercel Serverless Function. The verified fix is Fix: [jawj (maintainer)]: On Vercel's non-Edge serverless functions the WebSocket transport may fail to open; in that environment just use ordinary node-postgres (`pg`) instead of the serverless driver. Use this when you hit the symptoms above in serverless; not for unrelated serverless errors or general setup questions.

TL;DR

Fix: [jawj (maintainer)]: On Vercel's non-Edge serverless functions the WebSocket transport may fail to open; in that environment just use ordinary node-postgres (pg) instead of the serverless driver. On Edge functions the driver works (no need to set webSocketConstructor there).

The error

I'm getting the following error when using this on vercel

Fix

  1. [jawj (maintainer)]: On Vercel's non-Edge serverless functions the WebSocket transport may fail to open; in that environment just use ordinary node-postgres (pg) instead of the serverless driver.
  2. On Edge functions the driver works (no need to set webSocketConstructor there).

Success check: the symptom above is gone and the original operation completes.

When to use

  • You hit this exact symptom in serverless.
  • You want the verified fix without digging through the thread.

When not to use

  • A different error in serverless, even a similar-looking one.
  • General serverless setup or credential questions.

Compatibility

  • Serverless (neondatabase/serverless)

Variant phrasings

  • How to fix "websocket error on Vercel Serverless Function" in serverless
  • serverless: websocket error on Vercel Serverless Function
  • websocket problem on Vercel Serverless Function

Source

  • https://github.com/neondatabase/serverless/issues/15

Evidence

Asked in gh:neondatabase/serverless#15 (https://github.com/neondatabase/serverless/issues/15): Websocket error on Vercel Serverless Function. The issue report: Issue neondatabase/serverless#15 (closed, 12 comments):

I'm getting the following error when using this on vercel

Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: All attempts to open a WebSocket to connect to the database failed. If using Node, please install the `ws` package (or simply use the `pg` package instead).","reason":{"errorType":"Error","errorMessage":"All attempts to open a WebSocket to connect to the database failed. If using Node, please install the `ws` package (or simply use the `pg` package instead).","stack":["Error: All attempts to open a WebSocket to connect to the The verified answer: [jawj (maintainer)]: On Vercel's non-Edge serverless functions the WebSocket transport may fail to open; in that environment just use ordinary node-postgres (`pg`) instead of the serverless driver. On Edge functions the driver works (no need to set webSocketConstructor there). Local `npx vercel dev` was also updated to support both. No driver bug  -  environment limitation.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl___TVCCNjybydk6dyYYK6Yg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Websocket error on Vercel Serverless Function <span>— Vectle</span></a>