The Postgres adapter relies on the NOTIFY and LISTEN commands.
Every packet that is sent to multiple clients (e.g. io.to("room1").emit() or socket.broadcast.emit()) is:
The source code of this adapter can be found here.
npm install @socket.io/postgres-adapter pg
For TypeScript users, you might also need @types/pg.
const { Server } = require("socket.io");
const { createAdapter } = require("@socket.io/postgres-adapter");
const { Pool } = require("pg");
const io = new Server();
const pool = new Pool({
user: "postgres",
host: "localhost",
database: "postgres",
password: "changeit",
port: 5432,
});
pool.query(`
CREATE TABLE IF NOT EXISTS socket_io_attachments (
id bigserial UNIQUE,
created_at timestamptz DEFAULT NOW(),
payload bytea
);
`);
io.adapter(createAdapter(pool));
io.listen(3000);
| Name | Description | Default value |
|---|---|---|
uid |
the ID of this node | a random ID |
channelPrefix |
the prefix of the notification channel | socket.io |
tableName |
the name of the table for payloads over the 8000 bytes limit or containing binary data | socket_io_attachments |
payloadThreshold |
the threshold for the payload size in bytes | 8000 |
requestsTimeout |
the timeout for inter-server requests such as fetchSockets() or serverSideEmit() with ack |
5000 |
heartbeatInterval |
the number of ms between two heartbeats | 5000 |
heartbeatTimeout |
the number of ms without heartbeat before we consider a node down | 10000 |
cleanupInterval |
the number of ms between two cleanup queries | 30000 |
Yes. Failing to do so will result in HTTP 400 responses (you are reaching a server that is not aware of the Socket.IO session).
More information can be found here.
In case the connection to the Postgres server is severed, the packets will only be sent to the clients that are connected to the current server.
The Postgres emitter allows sending packets to the connected clients from another Node.js process:
npm install @socket.io/postgres-emitter pg
const { Emitter } = require("@socket.io/postgres-emitter");
const { Pool } = require("pg");
const pool = new Pool({
user: "postgres",
host: "localhost",
database: "postgres",
password: "changeit",
port: 5432,
});
const emitter = new Emitter(pool);
setInterval(() => {
emitter.emit("ping", new Date());
}, 1000);
Please refer to the cheatsheet here.
© 2014–2021 Automattic
Licensed under the MIT License.
https://socket.io/docs/v4/postgres-adapter