HTTP vs WebSocket modes
The Neon serverless driver supports two connection modes:- HTTP mode — Uses the
neonfunction to execute queries over HTTP. Faster for single queries and non-interactive transactions. No connection state is maintained between requests. - WebSocket mode — Uses the
Poolobject to establish a WebSocket connection. Required for session support, interactive transactions, ornode-postgrescompatibility.
| Use case | Recommended mode |
|---|---|
| Single queries | HTTP |
| Non-interactive transactions (batch of queries) | HTTP |
| Interactive transactions | WebSocket |
| Session-based features | WebSocket |
node-postgres compatibility | WebSocket |
Setting up credentials
Both modes use the same credentials setup.1
Install the driver via npm:
2
You’ll need to create a Postgres role to use with the driver.
Once you have these credentials, place them in environment variables:These can all be added to a unified Postgres connection URL for use by the driver:
Using HTTP mode
HTTP mode is the simplest way to execute queries. You must configure thefetchEndpoint to use PlanetScale’s SQL endpoint.
neon function returns a tagged template literal that automatically handles parameterized queries, protecting against SQL injection. See Neon’s HTTP mode documentation for additional configuration options.
Non-interactive transactions
HTTP mode supports non-interactive transactions where you send a batch of queries to be executed together. Use thetransaction function:
Using WebSocket mode
WebSocket mode provides a fullPool interface compatible with the pg library. See Neon’s WebSocket documentation for the full API reference. This mode requires additional configuration for PlanetScale connections.
1
When connecting, you must set the following configuration options:
2
Here’s a complete example:
In browser or edge environments that have a native
WebSocket global, you don’t need to import ws or set neonConfig.webSocketConstructor.Security
PlanetScale requiresSCRAM-SHA-256 for all authentication to Postgres servers.
We maintain this strict requirement for security purposes.
For WebSocket connections, you must set neonConfig.pipelineConnect = false;.
This adds a bit of additional latency, but is necessary to connect using SCRAM-SHA-256.
When this is "password" (the default value) it requires using cleartext password authentication, reducing connection security.
HTTP mode connections handle authentication automatically and don’t require this configuration.
