Closed medbejimbj closed 6 years ago
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)
Hello @kamilmysliwiec Otherwise, is there a way to make emit message (in a request after doing some treatments in my API rest services or other services) in the server side and not in client side ?
Thanks
Sure, use the server instance - https://docs.nestjs.com/websockets/gateways (Server)
Yes the problem is that when I do It in my source code I have this error message : TypeError: Cannot read property 'in' of null in addLike function
` import { Logger } from '../../common/services'; import { Action } from '../models/action.entiy'; import { ActionRepository } from '../repositories/action.repository'; import { ActionNameType } from '../enums'; import { EventService } from '../../event/services'; import { Event } from '../../event/models'; import { EventTypeService } from '../../event/services/event-type.service'; import { UserService } from './user.service'; import { WebSocketServer } from '@nestjs/websockets';
@Injectable()
export class ActionService {
@WebSocketServer() server;
constructor(@InjectRepository(ActionRepository) private actionRepository: Repository
/**
* Create like action
* @param userAction
*/
public async addLike(userAction: Action): Promise<Action> {
console.log(userAction);
const profileAction = await this.actionRepository.findOne({
where: {
creator: userAction.profile,
profile: userAction.creator,
course: userAction.course,
actionName: ActionNameType.like
}
});
if (profileAction) {
profileAction.actionName = ActionNameType.match;
userAction.actionName = ActionNameType.match;
await this.actionRepository.save(profileAction);
const result = await this.actionRepository.save(userAction);
this.server.in('1').emit('notifications', {text: 'You have a new message', subject: 'some message'});
await this.createOnMatchEvent(userAction);
return result;
} else {
return await this.actionRepository.save(userAction);
}
}`
Please, use StackOverflow for such issues.
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.
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;
} 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