Sample Data Server

Overview

The sample data server is a web and websocket server written Javascript and runnable using Node.js in Visual Studio, or separately as a server process.

Setup

To run the server, you need to setup Node.js environment. (see Javascript environment for more detail)

Make sure that Node.js is installed on your machine. Open the folder where the server will run in VSCode. Create a new node environment (package.json file) by executing.

npm init -y

Code

// app.js const express = require('express'); const { WebSocketServer, WebSocket } = require('ws'); const app = express(); const PORT = process.env.PORT || 3000; const PORT2 = process.env.PORT || 8881; let price = 100; function nextPrice(){ let rand = Math.random(); setTimeout(function(){ price = 100 + Math.random(); connections.forEach(ws=>{ ws.send(JSON.stringify({ price:price })); }) nextPrice(); }, rand*1000*10); } nextPrice(); // Middleware to parse incoming JSON payloads app.use(express.json()); app.get('/', (req, res) => { res.send('Hello World!'); }); app.get('/price.json', (req, res) => { res.send(JSON.stringify(price)); }); app.post('/price.json', (req, res) => { res.send(JSON.stringify(price)); }); function timeseries(req, res){ let data = [{"date":"2000-01-01","price":101.07085191017187},{"date":"2000-01-02","price":100.17881281454716},{"date":"2000-01-03","price":102.24741134846673},{"date":"2000-01-04","price":104.48270254774415},{"date":"2000-01-05","price":100.65210612031511},{"date":"2000-01-06","price":92.05546663621999},{"date":"2000-01-07","price":87.63709379845189},{"date":"2000-01-08","price":82.43592144065678},{"date":"2000-01-09","price":83.47201798726739},{"date":"2000-01-10","price":88.4846576494191},{"date":"2000-01-11","price":87.45655873833779},{"date":"2000-01-12","price":91.00019955094791},{"date":"2000-01-13","price":89.17285853918447},{"date":"2000-01-14","price":91.39220590888205},{"date":"2000-01-15","price":91.99847442550706},{"date":"2000-01-16","price":91.66544791931688},{"date":"2000-01-17","price":91.304365194521},{"date":"2000-01-18","price":91.86556179238566},{"date":"2000-01-19","price":99.65886314075522},{"date":"2000-01-20","price":96.77268817111658},{"date":"2000-01-21","price":89.3776666766099},{"date":"2000-01-22","price":89.73926945465917},{"date":"2000-01-23","price":93.12773052674953},{"date":"2000-01-24","price":91.2567639355405},{"date":"2000-01-25","price":101.84173681858516},{"date":"2000-01-26","price":104.50272646505813},{"date":"2000-01-27","price":108.80308795960394},{"date":"2000-01-28","price":114.19201187116661},{"date":"2000-01-29","price":111.27501877554238},{"date":"2000-01-30","price":109.91767317237823},{"date":"2000-01-31","price":112.37916929096872},{"date":"2000-02-01","price":113.88415014023457},{"date":"2000-02-02","price":121.13055268170429},{"date":"2000-02-03","price":129.5656155804022},{"date":"2000-02-04","price":127.79889542526271},{"date":"2000-02-05","price":126.20987891717112},{"date":"2000-02-06","price":128.7263179256943},{"date":"2000-02-07","price":129.4698762274108},{"date":"2000-02-08","price":127.51035263552775},{"date":"2000-02-09","price":129.6242502438568},{"date":"2000-02-10","price":139.19750707802743},{"date":"2000-02-11","price":147.12857584307665},{"date":"2000-02-12","price":149.3636854352662},{"date":"2000-02-13","price":154.08652701782796},{"date":"2000-02-14","price":153.5349751955933},{"date":"2000-02-15","price":147.24163275473245},{"date":"2000-02-16","price":152.19465567121418},{"date":"2000-02-17","price":160.31839504360272},{"date":"2000-02-18","price":167.4989064430082},{"date":"2000-02-19","price":148.02205799825845},{"date":"2000-02-20","price":153.11193626860808},{"date":"2000-02-21","price":156.65297273625146},{"date":"2000-02-22","price":153.15192368223921},{"date":"2000-02-23","price":165.4166042226599},{"date":"2000-02-24","price":169.65419627567107},{"date":"2000-02-25","price":162.98326308596307},{"date":"2000-02-26","price":169.47035194511267},{"date":"2000-02-27","price":172.46653248820633},{"date":"2000-02-28","price":191.9060003938629},{"date":"2000-03-01","price":185.36741205204544},{"date":"2000-03-02","price":197.381494690766},{"date":"2000-03-03","price":203.35546141158062},{"date":"2000-03-04","price":212.82058854161937},{"date":"2000-03-05","price":209.84916898852103},{"date":"2000-03-06","price":206.16055277535656},{"date":"2000-03-07","price":184.56521160438515},{"date":"2000-03-08","price":177.35084219561887},{"date":"2000-03-09","price":170.52069476467264},{"date":"2000-03-10","price":186.55840121625027},{"date":"2000-03-11","price":190.15153978884456},{"date":"2000-03-12","price":194.07585920703522},{"date":"2000-03-13","price":208.78006440131216},{"date":"2000-03-14","price":201.59097916087075},{"date":"2000-03-15","price":189.37162547249244},{"date":"2000-03-16","price":197.06042759096786},{"date":"2000-03-17","price":179.68677126917464},{"date":"2000-03-18","price":169.71399524342834},{"date":"2000-03-19","price":165.93764497581813},{"date":"2000-03-20","price":163.9192655788489},{"date":"2000-03-21","price":164.3565019403473},{"date":"2000-03-22","price":170.7578667886359},{"date":"2000-03-23","price":174.99078230200308},{"date":"2000-03-24","price":164.70390892036923},{"date":"2000-03-25","price":162.405692255675},{"date":"2000-03-26","price":164.46183314907225},{"date":"2000-03-27","price":153.9850938791475},{"date":"2000-03-28","price":159.6875112685819},{"date":"2000-03-28","price":158.83572874435905},{"date":"2000-03-29","price":162.1467597218385},{"date":"2000-03-30","price":150.62367811081336},{"date":"2000-03-31","price":151.8010987925942},{"date":"2000-04-01","price":151.06371119881135},{"date":"2000-04-02","price":154.34702853372784},{"date":"2000-04-03","price":156.0692361647026},{"date":"2000-04-04","price":160.88538853108108},{"date":"2000-04-05","price":158.15554402099772},{"date":"2000-04-06","price":152.2481496638002},{"date":"2000-04-07","price":154.1421805736591},{"date":"2000-04-08","price":170.43567191895374},{"date":"2000-04-09","price":160.5510971015889}]; res.send(JSON.stringify(data)); } app.get('/timeseries.json', (req, res) => { timeseries(req, res); }); app.post('/timeseries.json', (req, res) => { timeseries(req, res); }); // Start the server and listen on the specified port app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`); }); // Define the port for the server to listen on // Initialize the WebSocket server const wss = new WebSocketServer({ port: PORT2 }); console.log(`WebSocket server is running on ws://localhost:${PORT}`); let connections = []; // Event listener for new client connections wss.on('connection', (socket, request) => { connections.push(socket); // Get client info like IP address if needed const clientIp = request.socket.remoteAddress; //console.log(`🔌 New client connected from ${clientIp}. Total clients: ${wss.clients.size}`); // Send a welcome message to the newly connected client //socket.send(JSON.stringify({ type: 'info', message: 'Welcome to the WebSocket server!' })); // Event listener for incoming messages from this specific client socket.on('message', (rawData, isBinary) => { // Convert the raw buffer data to a readable string or object const message = isBinary ? rawData : rawData.toString(); //console.log(`Received message: ${message}`); // Broadcast the message to all other connected clients wss.clients.forEach((client) => { // Check that the client is open and is NOT the sender if (client !== socket & client.readyState === WebSocket.OPEN) { client.send(rawData, { binary: isBinary }); } }); }); // Event listener for when this specific client disconnects socket.on('close', (code, reason) => { //console.log(`Client disconnected (Code: ${code}, Reason: ${reason || 'None'}). Remaining clients: ${wss.clients.size}`); }); // Handle errors for this specific connection socket.on('error', (error) => { //console.error(`Client socket error:`, error); }); }); // Handle overall server errors wss.on('error', (error) => { console.error('WebSocket server error:', error); });