Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Neon · Module

Client.end

Client.end() closes a Neon serverless driver's Client connection. Call it during cleanup after WebSocket-based queries, sessions, or interactive transactions finish.

How it works

Client.end() closes the Neon serverless driver's WebSocket-backed client connection. Use it after the client is no longer needed, typically in cleanup code after queries or interactive transactions have completed.

Syntax

await client.end();

Returns

A promise that resolves when the client connection has been closed.

Key points

  • Client supports persistent session behavior, interactive transactions, and compatibility with the pg ecosystem.
  • Transport choice depends on whether the operation is a one-shot query or requires an interactive session.
  • The serverless driver avoids raw TCP and offers HTTP or WebSocket connectivity for supported runtimes.

Example: Close a Neon Client after querying

import { Client } from '@neondatabase/serverless';

const client = new Client(process.env.DATABASE_URL);
await client.connect();
try {
  const result = await client.query('select 1 as ok');
  console.log(result.rows);
} finally {
  await client.end();
}

Connects a WebSocket Client, runs a query, and closes the client in a finally block.

When to use it

Lifecycle cleanup

Close a WebSocket-backed client after a query or transaction completes.

Session termination

Release a client after using session state across multiple operations.

Error-safe shutdown

Ensure cleanup runs after success or failure with a finally block.

Interview questions

QWhat does Client.end() do?

Client.end() terminates the client connection after work is complete. A typical pattern is to call it in a finally block so cleanup occurs whether the query succeeds or fails.

Follow-up: Why might the Client be unnecessary for a single query? The HTTP driver is designed for independent one-shot queries, while Client is for sessions, interactive transactions, or pg compatibility.

QWhen should a Neon Client be used instead of the HTTP query function?

Neon's Client constructor uses the serverless driver's WebSocket transport. This provides session and transaction support and is compatible with the node-postgres API.

Follow-up: When should WebSockets be selected over HTTP? Select WebSockets for multi-step interactive transactions or sessions; select HTTP for independent queries.

QHow should Client lifecycle cleanup be handled?

The client should be closed after its final operation, commonly with await client.end() in cleanup code. This prevents the WebSocket-backed session from remaining open after application work finishes.

Follow-up: What alternative supports connection pooling? The Neon driver also provides Pool, which uses the same WebSocket-based mode for session and transaction scenarios.

  • Pool
  • Client
  • neon()
  • WebSockets
  • interactive transactions
  • node-postgres compatibility

Source: Neon serverless driver. Checked 2026-10-16.