How to use the postgres-adapter in Socket.io in node.js #4211
-
My current setup is as such: I have connected to my postgres database and i also have the websocket connected fine to the frontend. But I am not sure how to use the postgres-adapter as its suggested in the documentation: They say that you make a pool instead of a Client - thats fine for the db - then you say you use the io.adapter(createAdapter(pool)); The main functionality I want to get at is - when a change occurs on the postgresdb - then a notification gets send out just as I have currently - but then it should be connected inside the websocket for the websocket to send a message with the data to all connected clients. Has anyone implemented this?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The PostgreSQL adapter acts as a relay between several Socket.IO servers (so that a packet sent with 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);
}); |
Beta Was this translation helpful? Give feedback.
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:
And then:
Or if you want to broadcast to all clients directly: