Source: src/app.ts

Startup sequence

HTTP server

Socket.IO

Socket listeners are registered at startup and re-registered at equity market open (9:15 IST): See Socket server for event details.

Scheduled jobs

scheduleExchangeSessionJobs() registers all node-schedule cron jobs. See Scheduled jobs for the full timetable.

Market services initialization

initializeMarketServices() runs on startup:
  1. Ensures Zerodha market data streaming is active (initializeMarketDataIfNeeded)
  2. Starts recurring buy and trailing sell for each active index setting whose exchange is currently open
  3. Syncs the trailing-sell interval

Graceful shutdown

Triggered by SIGINT, SIGTERM, SIGBREAK, uncaughtException, or unhandledRejection:
  1. Cancel all node-schedule jobs
  2. Disconnect Zerodha WebSocket (handleZerodhaDisconnect)
  3. Stop all registered intervals (stopAllIntervals)
  4. Reset all recurring orders (resetAll)
  5. Close Socket.IO and HTTP server (10s force-exit timeout)

Error handling

Global Express error middleware:
  • 413 for payload-too-large errors
  • 500 with { success: false, message: 'Internal server error' } for all other errors

Database connection

import './config/database/index' is loaded at the top of app.ts before route registration. MongoDB connection is established asynchronously.