Skip to content

ServerAndEventStreamsFactory

reference
1 min readUpdated

Kind: Class

Source: packages/websockets/factories/server-and-event-streams-factory.ts

Part of: Websockets

ServerAndEventStreamsFactory creates a ServerAndEventStreamsHost<T> around an existing WebSocket server instance. It provides the WebSocket layer with a consistent host object that can expose the underlying server and its event-stream capabilities to the rest of the framework.

Methods

MethodSignatureReturns
createcreate(server: T)ServerAndEventStreamsHost<T>

Diagram

mermaid
graph LR
  A[WebSocket Adapter / Server] --> B[ServerAndEventStreamsFactory]
  B -->|create()| C[ServerAndEventStreamsHost<T>]
  C --> D[WebSocket Server Instance]
  C --> E[Event Stream Handlers]

Usage

ts
import { Server } from 'socket.io';
import { ServerAndEventStreamsFactory } from '@nestjs/websockets/factories/server-and-event-streams-factory';

const io = new Server(3000, {
  cors: {
    origin: '*',
  },
});

const factory = new ServerAndEventStreamsFactory();
const host = factory.create<Server>(io);

// Access the wrapped server through the host.
host.server.on('connection', socket => {
  socket.emit('connected', { message: 'Welcome!' });
});

AI Coding Instructions

  • Use this factory when framework code needs a ServerAndEventStreamsHost<T> rather than a raw WebSocket server instance.
  • Preserve the generic server type (T) so adapter-specific APIs, such as Socket.IO methods, remain type-safe.
  • Pass an already initialized server instance; this factory wraps server/event-stream infrastructure and should not be responsible for server startup configuration.
  • Keep adapter-specific behavior outside the factory—implement transport setup and connection options in the WebSocket adapter or bootstrap layer.
  • Treat this as framework infrastructure; prefer public WebSocket module APIs in application code when available.

How it works

ServerAndEventStreamsFactory

ServerAndEventStreamsFactory is an exported class with one static method, create<T = any>(server), which constructs a ServerAndEventStreamsHost<T> around a supplied server value. server-and-event-streams-factory.ts:4-5

Was this page helpful?

Download as PDF
ServerAndEventStreamsFactory — NestJS head-to-head