0

Client is sending object that contains multiple rooms to join so once socket starts streaming user should see event that are only related to that user based on rooms, So I use socket Rooms approach when event comes i checked if event.room are same as "rooms" sent from connected client start streaming to those channels.

client-Data = ["room1","room2"]

event-Data = {room: "room2",message:"This message is for room2 user only"}

So with above data user should joined both room and should expect this event because one room matched with event its not happening with below code user is getting event from the rooms that are not related to logged user. Any idea what is implemented wrong or better approach ?

server.js

io.on('connection', function(socket) {
   socket.on('joinChannel', function(rooms) {
       rooms.forEach(function,(room){
           socket.join(room);
           console.log('Room', room);
           oplogger.watch('riskAnalysisSLA.event', function(event) {
               console.log(event);
               if (event.room = room) {
                   io.sockets.in(room).emit('newMessage', event);
               }
           });
        });
    });
});

app.component.ts

ngOnInit() {
    this.streamService.getAllStream().subscribe(stream => {
        this.dataSource = new MatTableDataSource(stream);
        this.socket.on('newMessage', (event) => {
            this.dataSource.data.push(event.data);
            console.log("data",event.data);
            this.dataSource.data = [...this.dataSource.data]
        });
        this.socket.emit('joinChannel',["room1","room2"]);
    });
});
A.A Noman
  • 5,244
  • 9
  • 24
  • 46
hussain
  • 6,587
  • 18
  • 79
  • 152
  • What's your problem or question? A user *can* join multiple rooms. At quick glance, it looks like your syntax is correct for joining a room. – Justin Heath Dec 06 '17 at 19:08
  • I added some code and detail to question basically all users are getting events regardless of their rooms – hussain Dec 06 '17 at 19:11
  • This line `if (event.room = room) {` should be `if (event.room === room) {` and this `io.sockets.in(room).emit('newMessage', event);` should be `io.in(room).emit('newMessage', event);` – Justin Heath Dec 06 '17 at 19:11
  • Actually it's probably just the first line if the issue is that all users are getting all events. – Justin Heath Dec 06 '17 at 19:12
  • sorry i did not notice the syntax error , yeah so now events are not publishing to client at all – hussain Dec 06 '17 at 19:27
  • Move the `oplogger.watch` call out of the `io.on('connection'...` function callback. The in your event callback for `oplogger.watch` you shouldn't need to check if `event.room === room` and simply emit to `event.room`. If someone is in that room, they should get the message. If nobody is in that room, then nobody will get the message. – Justin Heath Dec 06 '17 at 19:35

0 Answers0