Reputation: 587
am trying to join user to room then send a massage to all users in that room, but its not work . any idea would help alot .
Server side :
let users = {};
let customerId = {};
io.on('connection', function (socket) {
socket.on('AllUsers' , function (data){
console.log(data);
customerId[socket.id] = data.userId
socket.join("customer");
});
io.sockets.in("customer").emit('chat',"hi");
});
Client side:
<script src = "/socket.io/socket.io.js"></script>
<script>
const socket = io();
socket.emit('AllUsers',{userId:'customer1323456'});
socket.on("chat", function (arg) {
console.log("clientererer" , arg);
const gg = document.getElementById('gg').textContent = arg ;
});
function myFunction() {
}
</script>
Upvotes: 2
Views: 812
Reputation: 1864
You can send messages to all users who has joined the room by:
io.of("/roomName").emit("eventName", "message");
for your case :
io.on('connection', function (socket) {
socket.on('AllUsers' , function (data){
console.log(data);
customerId[socket.id] = data.userId
socket.join("customer");
io.of("/customer").emit("chat", "hi");
});
});
for more info :
Upvotes: 1