Gathering detailed insights and metrics for socket.io-adapter-postgres
Gathering detailed insights and metrics for socket.io-adapter-postgres
Gathering detailed insights and metrics for socket.io-adapter-postgres
Gathering detailed insights and metrics for socket.io-adapter-postgres
Adapter to enable broadcasting of events to multiple separate socket.io server nodes.
npm install socket.io-adapter-postgres
Typescript
Module System
Node Version
NPM Version
69.5
Supply Chain
96.9
Quality
73.9
Maintenance
50
Vulnerability
99.1
License
JavaScript (99.84%)
Makefile (0.16%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
5 Stars
201 Commits
1 Forks
2 Watchers
1 Branches
1 Contributors
Updated on Jun 03, 2020
Latest Version
1.2.1
Package Id
socket.io-adapter-postgres@1.2.1
Unpacked Size
28.76 kB
Size
6.91 kB
File Count
4
NPM Version
6.11.2
Node Version
10.16.3
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
6
5
Derived from socket.io-redis.
1const io = require('socket.io')(3000); 2const postgres = require('socket.io-adapter-postgres'); 3io.adapter(postgres({ host: 'localhost', port: 5432 }));
By running socket.io with the socket.io-adapter-postgres
adapter you can run
multiple socket.io instances in different processes or servers that can
all broadcast and emit events to and from each other.
If you need to emit events to socket.io instances from a non-socket.io process, you should use socket.io-emitter.
uri
is a string like localhost:5432
where your PostgreSQL server
is located. For a list of options see below.
The following options are allowed:
key
: the name of the key to pub/sub events on as prefix (socket.io
)pubClient
: optional, the pg.Pool to publish events onsubClient
: optional, the pg.Client to subscribe to events onrequestsTimeout
: optional, after this timeout the adapter will stop waiting from responses to request (1000ms
)If you decide to supply pubClient
and subClient
, make sure you use
pg as a client or one
with an equivalent API.
The PostgreSQL adapter instances expose the following properties
that a regular Adapter
does not
uid
prefix
pubClient
subClient
requestsTimeout
Returns the list of client IDs connected to rooms
across all nodes. See Namespace#clients(fn:Function)
1io.of('/').adapter.clients((err, clients) => { 2 console.log(clients); // an array containing all connected socket ids 3}); 4 5io.of('/').adapter.clients(['room1', 'room2'], (err, clients) => { 6 console.log(clients); // an array containing socket ids in 'room1' and/or 'room2' 7}); 8 9// you can also use 10 11io.in('room3').clients((err, clients) => { 12 console.log(clients); // an array containing socket ids in 'room3' 13});
Returns the list of rooms the client with the given ID has joined (even on another node).
1io.of('/').adapter.clientRooms('<my-id>', (err, rooms) => { 2 if (err) { /* unknown id */ } 3 console.log(rooms); // an array containing every room a given id has joined. 4});
Returns the list of all rooms.
1io.of('/').adapter.allRooms((err, rooms) => { 2 console.log(rooms); // an array containing all rooms (accross every node) 3});
Makes the socket with the given id join the room. The callback will be called once the socket has joined the room, or with an err
argument if the socket was not found.
1io.of('/').adapter.remoteJoin('<my-id>', 'room1', (err) => { 2 if (err) { /* unknown id */ } 3 // success 4});
Makes the socket with the given id leave the room. The callback will be called once the socket has left the room, or with an err
argument if the socket was not found.
1io.of('/').adapter.remoteLeave('<my-id>', 'room1', (err) => { 2 if (err) { /* unknown id */ } 3 // success 4});
Makes the socket with the given id to get disconnected. If close
is set to true, it also closes the underlying socket. The callback will be called once the socket was disconnected, or with an err
argument if the socket was not found.
1io.of('/').adapter.remoteDisconnect('<my-id>', true, (err) => { 2 if (err) { /* unknown id */ } 3 // success 4});
Sends a request to every nodes, that will respond through the customHook
method.
1// on every node 2io.of('/').adapter.customHook = (data, cb) => { 3 cb('hello ' + data); 4} 5 6// then 7io.of('/').adapter.customRequest('john', function(err, replies){ 8 console.log(replies); // an array ['hello john', ...] with one element per node 9});
Access the pubClient
and subClient
properties of the
PostgreSQL Adapter instance to subscribe to its error
event:
1const postgres = require('socket.io-adapter-postgres'); 2const adapter = postgres('localhost:5432'); 3adapter.pubClient.on('error', function(){}); 4adapter.pubClient.on('connect', function(client){ client.on('error', function(){}); }); 5adapter.subClient.on('error', function(){});
The errors emitted from pubClient
and subClient
will
also be forwarded to the adapter instance:
1const io = require('socket.io')(3000); 2const redis = require('socket.io-redis'); 3io.adapter(redis({ host: 'localhost', port: 6379 })); 4io.of('/').adapter.on('error', function(){});
If you need to create a redisAdapter to a redis instance that has a password, use pub/sub options instead of passing a connection string.
1const redis = require('redis').createClient; 2const adapter = require('socket.io-redis'); 3const pub = redis(port, host, { auth_pass: "pwd" }); 4const sub = redis(port, host, { auth_pass: "pwd" }); 5io.adapter(adapter({ pubClient: pub, subClient: sub }));
1const io = require('socket.io')(3000); 2const Redis = require('ioredis'); 3 4const cluster = new Redis.Cluster([ 5 { 6 port: 6380, 7 host: '127.0.0.1' 8 }, 9 { 10 port: 6381, 11 host: '127.0.0.1' 12 } 13]); 14 15const adapter = require('socket.io-redis'); 16io.adapter(adapter({ pubClient: cluster, subClient: cluster }));
The socket.io-redis
adapter broadcasts and receives messages on particularly named Redis channels. For global broadcasts the channel name is:
prefix + '#' + namespace + '#'
In broadcasting to a single room the channel name is:
prefix + '#' + namespace + '#' + room + '#'
prefix
: The base channel name. Default value is socket.io
. Changed by setting opts.key
in adapter(opts)
constructornamespace
: See https://github.com/socketio/socket.io#namespace.room
: Used if targeting a specific room.A number of other libraries adopt this protocol including:
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/8 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
23 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More