Sunday 28 April 2019

Nodejs how to get array of socketio sockets from redis adapter

I am currently using redis adapter in my socketio setup I want to be able to get an array of sockets from redis adapter so that I can check the session and only send data to the array with the correct account id I have tried the following code but returns an empty array any help?NET.getDashboardSockets = () => { return new Promise((resolve, reject) => { const sockets = []; NET._io.of("/dashboard").adapter.clients((err, clients) => { if (clients.length > 0 && err == null) { for (let i = 0; i < clients.length; i++) { const client = clients[i]; const socketData = NET._io.of("/dashboard").to(client) if (socketData != null) { sockets.push(socketData); } } } resolve(sockets); }); }) } NET.getDashboardSocketsWithId = (Account) => { return new Promise((resolve, reject) => { const retsockets = []; NET.getDashboardSockets().then(sockets => { for (let i = 0; i < sockets.length; i++) { const socket = sockets[i]; if (socket.handshake != null) { const acctId = socket.handshake.session.accountId; if (acctId != null) { if (acctId == Account.getId()) { retsockets.push(socket); } } } } resolve(retsockets); }) }) }

Submitted April 28, 2019 at 02:34PM by MrHid6

No comments:

Post a Comment