'Notification send to user in nodeJS
Following code is for back end which is implemented in NodeJS for socket call.
const socket = require('socket.io');
let Users = [];
module.exports = function (server) {
const io = socket.listen(server);
io.sockets.on('connection', function (socket) {
socket.on('socket-call', function (userId) {
Users[userId] = socket.id;
socket.broadcast.emit('socket-call', {msg: 'login call'});
});
});
};
I want to send notification message to particular user as which id has been passed from front end. But it send notification to all user.
I have tried with following, It is not working. Please suggest me for the solution.
socket.broadcast.to(Users[userId]).emit('socket-call', {msg: 'login call'});
Solution 1:[1]
Try with
socket.emit('socket-call', userId);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tejas Gami |