UNPKG

@socket.io/postgres-adapter

Version:

The Socket.IO Postgres adapter, allowing to broadcast events between several Socket.IO servers

73 lines (72 loc) 2.31 kB
import { Pool } from "pg"; import { ClusterAdapterWithHeartbeat } from "socket.io-adapter"; import type { ClusterAdapterOptions, ClusterMessage, ClusterResponse, Offset, ServerId } from "socket.io-adapter"; export interface PostgresAdapterOptions { /** * the name of this node * @default a random id */ uid: string; /** * The prefix of the notification channel * @default "socket.io" */ channelPrefix: string; /** * The name of the table for payloads over the 8000 bytes limit or containing binary data * @default "socket_io_attachments" */ tableName: string; /** * The threshold for the payload size in bytes (see https://www.postgresql.org/docs/current/sql-notify.html) * @default 8000 */ payloadThreshold: number; /** * after this timeout the adapter will stop waiting from responses to request * @default 5000 */ requestsTimeout: number; /** * Number of ms between two cleanup queries * @default 30000 */ cleanupInterval: number; /** * Handler for errors. If undefined, the errors will be simply logged. * * @default undefined */ errorHandler: (err: Error) => void; } /** * Returns a function that will create a PostgresAdapter instance. * * @param pool - a pg.Pool instance * @param opts - additional options * * @public */ export declare function createAdapter(pool: Pool, opts?: Partial<PostgresAdapterOptions>): (nsp: any) => PostgresAdapter; export declare class PostgresAdapter extends ClusterAdapterWithHeartbeat { readonly channel: string; readonly tableName: string; payloadThreshold: number; errorHandler: (err: Error) => void; private readonly pool; /** * Adapter constructor. * * @param nsp - the namespace * @param pool - a pg.Pool instance * @param opts - additional options * * @public */ constructor(nsp: any, pool: Pool, opts?: Partial<PostgresAdapterOptions & ClusterAdapterOptions>); onEvent(event: any): Promise<any>; protected doPublish(message: ClusterMessage): Promise<Offset>; protected doPublishResponse(requesterUid: ServerId, response: ClusterResponse): Promise<void>; private _publish; private publishWithAttachment; }