question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about WebSocketGateway class - Emit to specific room from external component

See original GitHub issue

Hi,

I have a WebSocketGateway :

import { Injectable, UseFilters, UseGuards } from ‘@nestjs/common’; import { OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit, SubscribeMessage, WebSocketGateway, WebSocketServer, WsResponse, } from ‘@nestjs/websockets’;

import { Logger } from ‘…/…/common/services’; import { WsGuard } from ‘…/…/user/guards’; import { WsExceptionFilter } from ‘…/…/common/exception-filters’; import { AuthService } from ‘…/…/user/services’;

@Injectable() @UseGuards(WsGuard) @UseFilters(WsExceptionFilter) @WebSocketGateway({namespace: ‘messages’}) export class MessagesGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect { @WebSocketServer() server;

constructor(private logger: Logger, private authService: AuthService) {
}

afterInit(server) {
    this.logger.warn(`After init`);
}

public async handleConnection(client): Promise<any> {
    this.logger.log(`Client ${client.id} connected`);

    try {
        const user = await this.authService.retrieveUserFromAccessToken(client.handshake.query.auth_token);
        if (user) {
            client.join(user.id);
        }
    } catch (exception) {
        this.logger.warn(`User can not be authenticated`);
    }

}

public handleDisconnect(client): any {
    this.logger.log(`Client ${client.id} disconnected`);
}

@SubscribeMessage('save')
onEvent(client, message: any): WsResponse<any> {
    message.time = new Date();

    this.server.in('1').emit('notifications', {text: 'You have a new message', subject: message});

    client.broadcast.emit('messageSaved', message);

    return {event: 'messageSaved', data: message};
}

} I’m trying to send messages (emit ) to this specific room (“1” on this example) from other parts of the application (others components that can be or not inside the same module).

There is a way to do it without connecting via a socket.io client?

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Sep 5, 2018

Hi @medbejimbj, Honestly, I don’t understand. Just to clarify - in order to emit to the specific room, you have to use client instance (there is no other way)

0reactions
lock[bot]commented, Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nestjs Gateways emit an event to all connected sockets
This is a good question that many people may be searching if they can't quite find the appropriate documentation or don't understand the ......
Read more >
Gateways | NestJS - A progressive Node.js framework
This section covers the aspects of Nest that are specific to WebSockets. In Nest, a gateway is simply a class annotated with @WebSocketGateway()...
Read more >
How to use the @nestjs/websockets.WebSocketGateway ...
To help you get started, we've selected a few @nestjs/websockets.WebSocketGateway examples, based on popular ways it is used in public projects.
Read more >
How to emit an event when a checkbox is clicked using Socket ...
In this we have created a simple check box, When we check the checkbox it will emit one socket event which listens by...
Read more >
Communications Instant Messaging Server Release Notes
The XMPP WebSocket Gateway enables the Instant Messaging Server to support the ... Specifies the provider class for the External Service Discovery plugin....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found