Market Data Service is a high-performance financial data API that provides comprehensive Symbol prices of different markets through both RESTful endpoints and real-time WebSocket connections.

server.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const app = require('./app');
  2. const { testConnection } = require('./config/database');
  3. const logger = require('./utils/logger');
  4. const { createServer } = require('http');
  5. const { Server } = require('socket.io');
  6. const PORT = process.env.PORT || 3000;
  7. // Test database connection and start server
  8. const startServer = async () => {
  9. try {
  10. // Test database connection
  11. await testConnection();
  12. // Create HTTP server
  13. const server = createServer(app);
  14. // Initialize Socket.io
  15. const io = new Server(server, {
  16. cors: {
  17. origin: process.env.CORS_ORIGIN || "*",
  18. methods: ["GET", "POST"],
  19. credentials: true
  20. }
  21. });
  22. // Socket.io connection handling
  23. io.on('connection', (socket) => {
  24. logger.info(`Client connected: ${socket.id}`);
  25. // Handle subscription to symbols
  26. socket.on('subscribe', (symbols) => {
  27. if (Array.isArray(symbols)) {
  28. symbols.forEach(symbol => {
  29. socket.join(`symbol:${symbol}`);
  30. logger.info(`Client ${socket.id} subscribed to ${symbol}`);
  31. });
  32. } else {
  33. socket.join(`symbol:${symbols}`);
  34. logger.info(`Client ${socket.id} subscribed to ${symbols}`);
  35. }
  36. });
  37. // Handle unsubscription
  38. socket.on('unsubscribe', (symbols) => {
  39. if (Array.isArray(symbols)) {
  40. symbols.forEach(symbol => {
  41. socket.leave(`symbol:${symbol}`);
  42. logger.info(`Client ${socket.id} unsubscribed from ${symbol}`);
  43. });
  44. } else {
  45. socket.leave(`symbol:${symbols}`);
  46. logger.info(`Client ${socket.id} unsubscribed from ${symbols}`);
  47. }
  48. });
  49. // Handle disconnect
  50. socket.on('disconnect', () => {
  51. logger.info(`Client disconnected: ${socket.id}`);
  52. });
  53. });
  54. // Make io accessible to routes/controllers
  55. app.set('io', io);
  56. // Start the server
  57. server.listen(PORT, () => {
  58. logger.info(`Market Data Service is running on port ${PORT}`);
  59. logger.info(`Environment: ${process.env.NODE_ENV || 'development'}`);
  60. logger.info(`Health check available at: http://localhost:${PORT}/health`);
  61. logger.info(`WebSocket server ready for connections`);
  62. });
  63. // Graceful shutdown
  64. process.on('SIGTERM', () => {
  65. logger.info('SIGTERM received, shutting down gracefully');
  66. server.close(() => {
  67. logger.info('Process terminated');
  68. process.exit(0);
  69. });
  70. });
  71. process.on('SIGINT', () => {
  72. logger.info('SIGINT received, shutting down gracefully');
  73. server.close(() => {
  74. logger.info('Process terminated');
  75. process.exit(0);
  76. });
  77. });
  78. } catch (error) {
  79. logger.error('Failed to start server:', error);
  80. process.exit(1);
  81. }
  82. };
  83. // Handle uncaught exceptions
  84. process.on('uncaughtException', (error) => {
  85. logger.error('Uncaught Exception:', error);
  86. process.exit(1);
  87. });
  88. // Handle unhandled promise rejections
  89. process.on('unhandledRejection', (reason, promise) => {
  90. logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
  91. process.exit(1);
  92. });
  93. startServer();