Provide WebSocket support in older Node.js
A Node.js 21 or earlier application uses Pool or Client for sessions or interactive transactions.
Neon · Pipe
neonConfig.webSocketConstructor configures the WebSocket implementation used by Neon’s serverless driver. Set it before constructing a WebSocket-based Pool or Client when the runtime lacks a built-in WebSocket.
neonConfig.webSocketConstructor lets the Neon serverless driver use a supplied WebSocket implementation when a runtime lacks a built-in one. Set it before constructing a Pool or Client; Node.js 22 and later normally provide WebSocket themselves.
neonConfig.webSocketConstructor = WebSocketConstructor;No value; it configures the WebSocket constructor used by the Neon serverless driver.
Pool and Client constructors.ws package.neonConfig from @neondatabase/serverless alongside the WebSocket client you plan to use.neonConfig.webSocketConstructor before opening a driver connection.import { Pool, neonConfig } from '@neondatabase/serverless';
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const result = await pool.query('SELECT 1');
console.log(result.rows);
await pool.end();Assign the ws implementation before creating a WebSocket-based Pool, then close the pool after querying.
A Node.js 21 or earlier application uses Pool or Client for sessions or interactive transactions.
A runtime does not expose a global WebSocket implementation but the application needs the Neon driver’s WebSocket transport.
An application uses the ws package as its WebSocket implementation for a Neon connection.
neonConfig.webSocketConstructor?Set neonConfig.webSocketConstructor to a compatible constructor, such as the ws export, before creating the Pool or Client. This supplies WebSocket support to runtimes that do not provide it globally.
Follow-up: When can the configuration be omitted? It can be omitted in Node.js 22 and later because those versions include a built-in WebSocket.
The setting matters when using the serverless driver's WebSocket-based Pool or Client in a runtime without built-in WebSocket support. It is not needed merely to use the HTTP neon() function.
Follow-up: Which transport fits independent one-shot queries? Use the HTTP driver, because those operations do not require a persistent WebSocket session.
Use a WebSocket constructor when the application needs Pool or Client functionality and the runtime lacks WebSocket support. Those clients provide session and transaction behavior over WebSockets.
Follow-up: Why not always choose WebSockets? HTTP is preferable for one-shot queries and non-interactive transactions, while WebSockets are for interactive transactions, sessions, or node-postgres compatibility.
PoolClientneonConfigneon()WebSocketsHTTP