This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
4
Websockets - how do I subscribe to channels dynamically?
Post Body
Hi,
I have added a basic socket based chat to my nestJS API and am unsure how to subscribe users to only the chat groups they are part of.
I think it's something to do with the '@Subscribe('chat') so I'm assuming I might need to catch the incoming userID, query the database for their groups and then loop and subscribe but thats just a guess.
Here's my code;
@WebSocketGateway()
export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer() server;
users = 0;
constructor(
private chatService: ChatService,
) {
}
async handleConnection(){
this.users ;
this.server.emit('users', this.users);
}
async handleDisconnect(){
this.users--;
this.server.emit('users', this.users);
}
@SubscribeMessage('chat')
async onChat(client, message){
message.content = message.content.replace(/'/g, "\\'");
await this.chatService.save(message);
client.broadcast.emit('chat', message);
}
}
Author
Account Strength
90%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
691
Link Karma
293
Comment Karma
398
Profile updated: 2 days ago
Posts updated: 3 weeks ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Nestjs_fram...