Skip to content

How to use the postgres-adapter in Socket.io in node.js #4211

Answered by darrachequesne
uhakdt asked this question in Q&A
Discussion options

You must be logged in to vote

The PostgreSQL adapter acts as a relay between several Socket.IO servers (so that a packet sent with io.emit() gets broadcasted to all connected clients).

Reference: https://socket.io/docs/v4/postgres-adapter/

If I understand your use case correctly, you should be able to use a room:

socket.on('my message', (msg) => {
  socket.join('room for notification listeners');
});

And then:

client.on('notification', (msg) => {
  io.to('room for notification listeners').emit('new notification', msg.payload);
});

Or if you want to broadcast to all clients directly:

client.on('notification', (msg) => {
  io.emit('new notification', msg.payload);
});

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@uhakdt
Comment options

@rohitbrleaf
Comment options

@darrachequesne
Comment options

@izzuddinraffar
Comment options

@darrachequesne
Comment options

Answer selected by uhakdt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants