EdgeStream
Starting the Server
edgeStream.start() initiates all registered servers concurrently. Each server calls transport.connect(); once connected, incoming messages begin flowing through the pipeline.
Start Flow
// EdgeStream.start() — starts all servers concurrently
async start(): Promise<void> {
if (this.status === 'running') return;
this.status = 'starting';
const startPromises = Array.from(this.servers.values()).map(s => s.start());
await Promise.all(startPromises); // all servers start concurrently
this.status = 'running';
this.emit('stream:started', { timestamp: new Date() });
}
Typical Application Startup
// Application bootstrap
async function initializeEdgeStream() {
const stream = createEdgeStream({ logLevel: 'info' });
// 1. Register server(s)
stream.registerServer({ id: 'bas', type: 'bas', ... });
// 2. Configure hooks
const server = stream.server('bas')!;
server.incomingPipeline.addHook(new NormalizationHook());
// 3. Register subscribers
const sub = stream.subscribe('bas', 'workflow.*', workflowHandler);
// 4. Listen to lifecycle events
stream.on('stream:started', () => console.log('EdgeStream is running'));
stream.on('server:connected', (e) => console.log('Server connected:', e.serverId));
stream.on('server:error', (e) => console.error('Server error:', e.data?.error));
// 5. Start — transport connects, messages begin flowing
try {
await stream.start();
console.log('EdgeStream started successfully');
} catch (error) {
console.error('Failed to start EdgeStream:', error);
// Handle startup failure (show offline indicator, retry logic, etc.)
}
return stream;
}
Status Transitions
| Status | Meaning | Transitions To |
|---|---|---|
'idle' | Not started yet | 'starting' on start() |
'starting' | Connecting transports | 'running' on success, 'error' on failure |
'running' | Fully operational | 'stopping' on stop() |
'stopping' | Draining and disconnecting | 'idle' |
'error' | Failed start or runtime error | Manual intervention needed |
Idempotent Start
// Calling start() when already running is a no-op
await stream.start(); // connects
await stream.start(); // returns immediately — already running
console.log(stream.status); // 'running'