Skip to content

Server

createServer() is the single entry point for every Connectum service -- it wires up transports, interceptors, protocols, and graceful shutdown in one call.

Quick Start

typescript
import { createServer } from '@connectum/core';
import { Healthcheck, healthcheckManager, ServingStatus } from '@connectum/healthcheck';
import { Reflection } from '@connectum/reflection';
import { createDefaultInterceptors } from '@connectum/interceptors';
import routes from '#gen/routes.js';

const server = createServer({
  services: [routes],
  port: 5000,
  protocols: [Healthcheck({ httpEnabled: true }), Reflection()],
  interceptors: createDefaultInterceptors(),
  shutdown: { autoShutdown: true, timeout: 30_000 },
});

server.on('ready', () => healthcheckManager.update(ServingStatus.SERVING));
server.on('stop', () => console.log('Server stopped'));

await server.start();

Key Concepts

Server States

Every server follows a deterministic state machine:

StateDescription
createdServer object constructed, not yet listening
startingBinding port, initializing protocols
runningAccepting requests
stoppingDraining connections, executing shutdown hooks
stoppedFully shut down, all resources released

Lifecycle Events

EventFires when
startServer begins the start sequence
readyServer is bound and accepting requests
stoppingShutdown initiated (signal or manual stop())
stopServer is fully stopped
errorAn error occurs during start or shutdown

shutdownSignal

The server exposes an AbortSignal via server.shutdownSignal. It is aborted when shutdown begins -- pass it to streaming handlers and background workers so they can cancel gracefully.

typescript
server.on('ready', () => {
  startBackgroundWorker(server.shutdownSignal);
});

Learn More