I have a REST API, I want to send event to the client via websocket.
How to inject websocket instance in controller or another component?
Better solution is to create global module. You can then emit events from any other module/controller. A. Afir approach will create multiple instances of Gateway if you try to use it in other modules.
Note: This is just simplest solution
Create socket.module.ts
import { Module, Global } from '#nestjs/common';
import { SocketService } from './socket.service';
#Global()
#Module({
controllers: [],
providers: [SocketService],
exports: [SocketService],
})
export class SocketModule {}
socket.service.ts
import { Injectable } from '#nestjs/common';
import { Server } from 'socket.io';
#Injectable()
export class SocketService {
public socket: Server = null;
}
app.gateway.ts see afterInit function
import { WebSocketGateway, OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect, WebSocketServer } from '#nestjs/websockets';
import { Logger } from '#nestjs/common';
import { Server, Socket } from 'socket.io';
import { SocketService } from './socket/socket.service';
#WebSocketGateway()
export class AppGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(private socketService: SocketService){
}
#WebSocketServer() public server: Server;
private logger: Logger = new Logger('AppGateway');
afterInit(server: Server) {
this.socketService.socket = server;
}
handleDisconnect(client: Socket) {
this.logger.log(`Client disconnected: ${client.id}`);
}
handleConnection(client: Socket, ...args: any[]) {
this.logger.log(`Client connected: ${client.id}`);
}
}
Then import SocketModule into AppModule and you can use Socket service everywhere.
class Gateway can be injected in another component, and use the server instance.
#Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly messageGateway: MessageGateway
) {}
#Get()
async getHello() {
this.messageGateway.server.emit('messages', 'Hello from REST API');
return this.appService.getHello();
}
}
I suppose that #Raold missed a fact in the documentation:
Gateways should not use request-scoped providers because they must act as singletons. Each gateway encapsulates a real socket and cannot be instantiated multiple times.
So it means that we can neither instantiate the gateway class multiple times nor do it explicitly using injection scopes features.
So creating just only one gateway for one namespaces will be right and it will produce only one instance of the websocket or socket.io server.
Related
I'm trying to authenticate and check permission of a user while establishing a websocket connection in a NestJS application.
I've found this discussion which recommends to make use of NestJS Websocket adapter. You can perform the token validation in the options.allowRequest callback as below.
export class AuthenticatedSocketIoAdapter extends IoAdapter {
private readonly authService: AuthService;
constructor(private app: INestApplicationContext) {
super(app);
this.authService = app.get(AuthService);
}
createIOServer(port: number, options?: SocketIO.ServerOptions): any {
options.allowRequest = async (request, allowFunction) => {
const token = request.headers.authorization.replace('Bearer ', '');
const verified = this.authService.verifyToken(token);
if (verified) {
return allowFunction(null, true);
}
return allowFunction('Unauthorized', false);
};
return super.createIOServer(port, options);
}
}
I have a problem however with the dependency injection in the websocket adapter. The IoAdapter's constructor has an INestApplicationContext parameter from which I'm trying to get back the AuthService using app.get(AuthService) as you can see above.
The AuthService injects two other services, a UserService and the JwtService to check the JWT token. My problem is that those services remained not defined in that context.
#Injectable()
export class AuthService {
constructor(private usersService: UsersService, private jwtService: JwtService) {}
verifyToken(token: string): boolean {
// Problem: this.jwtService is undefined
const user = this.jwtService.verify(token, { publicKey });
// ... check user has permissions and return result
}
For info, the AuthService is in another module than the one which defines the Websocket. I also tried to import the AuthService (and its dependencies) in the current module but that didn't help.
Is that possible to make use the service using the app.get() method?
I could solve the DI issue by using app.resolve() instead of app.get()
export class AuthenticatedSocketIoAdapter extends IoAdapter {
private authService: AuthService;
constructor(private app: INestApplicationContext) {
super(app);
app.resolve<AuthService>(AuthService).then((authService) => {
this.authService = authService;
});
}
}
This solved the jwtService injected in the AuthService being undefined.
I used shared module with dynamic configuration in application.
Shared module contains interceptor and service which contains client configured from shared parameters.
I injected the service into the interceptor by predefined name (importing SharedModule dynamically into SecondAppModule). So client name can have different value. Inside of service I need to know the clients name before client injecting. Right now it is hard coded:
#Injectable()
export class SumClientService {
constructor(#Inject('MATH_SERVICE') private client: ClientProxy) {
console.log('[SumClientService] - created')
}
sumCalculation(row: number[]): Observable<number> {
return this.client.send<number>({ cmd: 'sum' }, row);
}
}
Question
Is there any ways to load service from context by name in case name known at construction time only?
I've detected two ways at list to past name as a parameter into service without corrupting DI managed by nest.js. But I have no idea how to get access to module context for loading service by specified name (the code of idea fragment is below)
#Injectable()
export class SumClientService {
constructor(#Inject('service_name') private name: string) {
console.log('[SumClientService] - created')
}
client: (clientName: string) => ClientProxy = (clientName: string): ClientProxy => // TODO load by clientName real client from `nest.js` context
// ...
}
P.S.
The idea is to use multiple clients in the same application. I considered scenario one client per module for the first time.
git code
There are few options to create microservice clients in dynamic module:
1st option
in module configuration level:
import { Module } from '#nestjs/common';
import { ClientModule } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
#Module({
imports: [
ClientModule.register([
{ 'NAME_FOR_DI', ...configForYourClient }
])
],
...
})
...
usage:
import { Inject } from '#nestjs/common';
import { ClientProxy } from '#nestjs/microservices';
...
constructor(#Inject('NAME_FOR_DI') private readonly client: ClientProxy) { }
2st option
For dynamically configured module:
import { Module, DynamicModule } from '#nestjs/common';
import { ClientModule } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
#Module({})
export class YourModule{
static register(): DynamicModule {
return {
module: YourModule,
imports: [
ClientModule.register([
{ 'NAME_FOR_DI', ...configForYourClient }
])
],
}
}
}
3st option
import { ClientProxy, ClientProxyFactory } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
...
private client: ClientProxy = ClientProxyFactory.create(configForYourClient)
Following second option it can be created in service directly or registered for module specifying DI key in providers, like:
import { Module } from '#nestjs/common';
import { ClientProxyFactory } from '#nestjs/microservices';
import { configForYourClient } from '../configs';
#Module({
providers: [
{
provide: 'NAME_FOR_DI',
useValue: ClientProxyFactory.create(configForYourClient)
},
],
...
})
...
with following injection by specified key.
P.S.
I've described here general idea for dynamic injection. This idea can be enriched with different combinations (injecting list of values for the single provider, or injecting list of configurations into ClientModule.register where you should apply some corrections in you DI approach)
i created sharedService it works perfectly , i can shared data from one component to another (this both are irrelevant component in different module).
Data Transfer as follows:
AdminDashboard.Component (update value) ===> conference.component (get new updated value)
problem : when i refresh my conference.component i lost the value
EventService.ts
import { Injectable } from '#angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { importExpr } from '#angular/compiler/src/output/output_ast';
import {Events} from '../models/event.model'
#Injectable()
export class EventService {
private dataSource = new BehaviorSubject(null);
sendMessage(data) {
this.dataSource.next(data);
}
getMessage(): Observable<any> {
return this.dataSource.asObservable();
}
}
dashboard.component (url /dashboard)
on Button Click msg() method called , which updated BehaviourSubjectvalue.
import { Component, OnInit} from '#angular/core';
import { NgForm } from '#angular/forms';
import { EventService } from '../../shared/sharedServies/eventService.service';
export class AdminDashboardComponent implements OnInit {
constructor( private testEventService: EventService){ }
msg() {
debugger
this.testEventService.sendMessage('Message from Home Component to App
Component!');
}
}
conference.component (url /conference)
Here , i hold value in message and bind to ui.
import { Component, OnInit } from '#angular/core';
import { EventService } from'../../shared/sharedServies/eventService.service';
import { Subscription } from 'rxjs/Subscription';
export class ViewconferenceComponent implements OnInit {
message: any;
constructor(private EventService: EventService) {
this.subscription = this.EventService.getMessage().subscribe(message => {
console.log(message)
this.message = message;
});
}
}
Question :
when i get data on /conference page , at this when i refresh the
service holded value is lost , i didn't understand what this happens.
also i need to add json to sharedService , how it will achive?
This is expected since when you "switch" components they are destroyed. You could work around this quickly by adding state variables to your service.
Personally, I encourage you to make use of some state library like ngRx https://github.com/ngrx/platform
I am attempting to make a call to the server using promises. When trying to add my parameters, it comes out as 'object%20Object'
Here is the call
import { Injectable } from '#angular/core';
import { Http } from '#angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import { User } from '../models/user';
#Injectable()
export class UserService {
private baseUserUrl = 'api/User/'
constructor(private http: Http) { }
getUsers(currentPage: number): Promise<User[]> {
return this.http.get(this.baseUserUrl + 'GetUsers?currentPage=' + currentPage)
.map(resp => resp.json() as User[])
.toPromise()
}
}
I was accidentally passing an object into the method, so I wasn't accessing the property, I was accessing the object. I fixed that and removed the object and passed a property.
Hello I'm faily new with Finatra and wanted to know if there is a way to validate that a request was made using the https protocol?
If you want to always use HTTPS you can disable the http server server
import com.twitter.finagle.Http
import com.twitter.finatra.http.HttpServer
import com.twitter.finatra.http.routing.HttpRouter
object ExampleHttpsServerMain extends ExampleHttpsServer
class ExampleHttpsServer
extends HttpServer
with Tls {
override val defaultHttpPort: String = "" // disable the default HTTP port
override val defaultHttpsPort: String = ":443"
override def configureHttp(router: HttpRouter): Unit = {
router
.add[ExampleController]
}
}
But if you want for some specific controller or route to check if it is https for example Login should only be used via https then you can define Filters for example:
Per Route
class ExampleController #Inject()(
exampleService: ExampleService
) extends Controller {
filter[ExampleFilter].get("/ping") { request: Request =>
"pong"
}
filter[ExampleFilter]
.filter[AnotherExampleFilter]
.get("/name") { request: Request =>
response.ok.body("Bob")
}
filter(new OtherFilter).post("/foo") { request: Request =>
exampleService.do(request)
"bar"
}
}
Per Controller
import DoEverythingModule
import ExampleController
import ExampleFilter
import com.twitter.finagle.http.Request
import com.twitter.finatra.http.filters.AccessLoggingFilter
import com.twitter.finatra.http.routing.HttpRouter
import com.twitter.finatra.http.{Controller, HttpServer}
object ExampleServerMain extends ExampleServer
class ExampleServer extends HttpServer {
override val modules = Seq(
DoEverythingModule)
override def configureHttp(router: HttpRouter) {
router
.add[ExampleFilter, ExampleController]
}
}
If you check all implementations of SimpleFilter you may find some filters that you can reuse or base your filter from.