Integrate GLSP WebSocketServerLauncher with NestJS Gateway #1415
-
Hello there, I'm currently trying to integrate GLSP Server with NestJS framework. Here is my code: import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import {
ServerModule,
SocketLaunchOptions,
WebSocketServerLauncher,
createAppModule,
createSocketCliParser,
defaultLaunchOptions
} from '@eclipse-glsp/server/node';
import { Container } from 'inversify';
import { SomeService } from 'src/some/some.service';
import { MyDiagramModule } from '../my-glsp/diagram/my-diagram-module';
@Injectable()
export class GLSPService implements OnModuleInit {
constructor(@Inject(SomeService) private readonly someService: SomeService) {}
onModuleInit() {
this.launch().catch((error) => console.error('Error in GLSP server:', error));
}
async launch() {
// define container options
const options = createSocketCliParser<SocketLaunchOptions>({
...defaultLaunchOptions,
port: 3001,
host: '127.0.0.1'
}).parse();
// create a new inversify container
const container = new Container();
// load a new GLSP app module
container.load(createAppModule(options));
// define NestJS services that can be used in the GLSP diagram module
const services = {
someService: this.someService
};
// create a GLSP Server Module with the Diagram Module (this will use a custom server module and diagram module for my diagram language support)
const serverModule = new ServerModule().configureDiagramModule(new MyDiagramModule(services));
// create a new WebSocketServerLauncher
const launcher = container.resolve(WebSocketServerLauncher);
// configure the server module and start the server
await launcher.configure(serverModule);
await launcher.start({ port: options.port, host: options.host, path: 'glsp' });
}
} But, the problems start when I want to communicate MyDiagramModule with other services of my NestJS server. I can use them by directly calling them, but can't get user related information throw the NestJS guards from the WebSocket messages (actions / operations of GLSP). Is there a way to integrate WebSocketServerLauncher with the Gateways of NestJS? So in this way the flow of messages sended and received by the WebSocket can pass throw NestJS workflow. Which other ways to integrate GLSP with NestJS are there? Thanks, cheers. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @giuliano-marinelli. Unfortunately, I'm not really familiar with the NestJS framework but what exactly do you mean by:
Is there a particular error that you are receiving? As you can see the |
Beta Was this translation helpful? Give feedback.
Thanks for the answer!
I found that it's not possible to integrate it directly into the NestJS workflow because both GLSP and NestJS use Containers, which instantiate their own classes. If I decorate the WebSocketLauncher with both @Injectable decorators, it will create one instance in each framework's containers.
Anyway, my problem was not that. Instead, I was trying to communicate the GLSP Server with my NestJS services to get the models from the database and needed to authenticate in some way.
Finally, I solved this by making custom Client Sessions in which I add authentication data sent from ws at the 'sec-websocket-protocol', and then make custom WebSocketServerLauncher and GLSPServe…