The @socket.io/postgres-adapter
package allows broadcasting packets between multiple Socket.IO servers.
Supported features:
Table of contents
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 events (
id bigserial UNIQUE,
created_at timestamptz DEFAULT NOW(),
payload bytea
);
`);
io.adapter(createAdapter(pool));
io.listen(3000);