With Angular how can I subscribe to an array of objects like this one:
[
'abc/user2': { room: 'abc/user2', user: 'user2' },
'abc/admin': { room: 'abc/admin', user: 'admin' }
]
Currently this doesn't work :
this.socketService.onRoom().subscribe(rooms => {
this.rooms = rooms;
});
With socketService:
public onRoom() {
return new Observable<any>(observer => {
this.socket.on('room', (data: any) => observer.next(data));
});
}
Edit:
Mybad, it seems that problem comes from the server (NodeJS/express/socket.io) :
chat-server.ts:
public rooms: Array<{room: string, user:string}> = [];
(...)
this.rooms[room] = {room: room, user: socket.user};
this.io.emit('room', this.rooms)
console.log('[server]rooms :', this.rooms)
Server side, console.log print :
[
'abc/user2': { room: 'abc/user2', user: 'user2' },
'abc/admin': { room: 'abc/admin', user: 'admin' }
]
But clientside in socketService, if i do a console.log(data) here :
public onRoom() {
return new Observable<any>(observer => {
this.socket.on('room', (data: any) => {
console.log(data)
observer.next(data)
});
});
}
It return :
[]
But if I emit serverside:
this.io.emit('room', Object.keys(this.rooms))
Clientside it return:
['abc/user2', 'abc/admin']
It's look like i can't emit an array of object from socket.io ?