@nestjs/websockets
Version:
WebSockets for NestJS: gateways with message handlers, guards, pipes and interceptors, on Socket.IO or ws.
106 lines (105 loc) • 5.38 kB
JavaScript
import { iterate } from 'iterare';
import { GATEWAY_METADATA } from './constants.js';
import { ExceptionFiltersContext } from './context/exception-filters-context.js';
import { WsContextCreator } from './context/ws-context-creator.js';
import { WsProxy } from './context/ws-proxy.js';
import { SocketServerProvider } from './socket-server-provider.js';
import { SocketsContainer } from './sockets-container.js';
import { WebSocketsController } from './web-sockets-controller.js';
import { Injector, GuardsConsumer, GuardsContextCreator, loadAdapter, InterceptorsConsumer, InterceptorsContextCreator, PipesConsumer, PipesContextCreator, } from '@nestjs/core/internal';
export class SocketModule {
socketsContainer = new SocketsContainer();
applicationConfig;
webSocketsController;
adapterInitPromise = null;
httpServer;
appOptions;
injector;
isAcceptingClients = true;
register(container, applicationConfig, graphInspector, appOptions, httpServer) {
this.applicationConfig = applicationConfig;
this.appOptions = appOptions;
this.httpServer = httpServer;
this.injector = new Injector({
preview: container.contextOptions?.preview,
instanceDecorator: container.contextOptions?.instrument?.instanceDecorator,
});
const exceptionFiltersContext = new ExceptionFiltersContext(container);
const contextCreator = this.getContextCreator(container, applicationConfig, exceptionFiltersContext);
const serverProvider = new SocketServerProvider(this.socketsContainer, applicationConfig, !!httpServer);
this.webSocketsController = new WebSocketsController(serverProvider, applicationConfig, contextCreator, container, this.injector, exceptionFiltersContext, graphInspector, this.appOptions, () => this.isAcceptingClients);
const modules = container.getModules();
return Promise.all(iterate(modules.entries())
.map(([moduleName, { providers }]) => this.connectAllGateways(providers, moduleName))
.toArray());
}
connectAllGateways(providers, moduleName) {
return Promise.all(iterate(providers.values())
.filter(wrapper => wrapper && !wrapper.isNotMetatype)
.map(wrapper => this.connectGatewayToServer(wrapper, moduleName))
.toArray());
}
async connectGatewayToServer(wrapper, moduleName) {
const { instance, metatype } = wrapper;
const metadataKeys = Reflect.getMetadataKeys(metatype);
if (!metadataKeys.includes(GATEWAY_METADATA)) {
return;
}
// Memoize the initialization promise so concurrent gateway connections
// share a single adapter instead of racing to create their own. Every
// gateway waits for it, even once the adapter is ready, so that gateways
// connect in the order they are declared in: of gateways that share a
// server, the one connected first creates it with its own options.
this.adapterInitPromise ??= this.initializeAdapter();
await this.adapterInitPromise;
this.webSocketsController.connectGatewayToServer(wrapper, moduleName);
}
/**
* Makes the gateways turn away the clients that connect to them, until
* "acceptClients()" is called. The servers of the gateways are created, and
* those with their own port are listening, as soon as the gateways are
* registered. The application calls this before it registers them, and
* "acceptClients()" once its lifecycle hooks ("onModuleInit",
* "onApplicationBootstrap") have completed, so that no client is served by
* providers that are not initialized yet.
*/
rejectClients() {
this.isAcceptingClients = false;
}
acceptClients() {
this.isAcceptingClients = true;
}
async close() {
this.rejectClients();
if (!this.applicationConfig) {
return;
}
const adapter = this.applicationConfig.getIoAdapter();
if (!adapter) {
return;
}
const servers = this.socketsContainer.getAll();
await Promise.all(iterate(servers.values())
.filter(({ server }) => server)
.map(async ({ server }) => adapter.close(server)));
await adapter.dispose?.();
this.socketsContainer.clear();
}
async initializeAdapter() {
const forceCloseConnections = this.appOptions
.forceCloseConnections;
const adapter = this.applicationConfig.getIoAdapter();
if (adapter) {
adapter.forceCloseConnections =
forceCloseConnections;
return;
}
const { IoAdapter } = await loadAdapter('@nestjs/platform-socket.io', 'WebSockets', () => import('@nestjs/platform-socket.io'));
const ioAdapter = new IoAdapter(this.httpServer);
ioAdapter.forceCloseConnections = forceCloseConnections;
this.applicationConfig.setIoAdapter(ioAdapter);
}
getContextCreator(container, config, exceptionFiltersContext) {
return new WsContextCreator(new WsProxy(), exceptionFiltersContext, new PipesContextCreator(container, config), new PipesConsumer(), new GuardsContextCreator(container, config), new GuardsConsumer(), new InterceptorsContextCreator(container, config), new InterceptorsConsumer());
}
}