Back to articles
AI & Cloud

Build a Real-Time Stock Monitoring System with Grafana and WebSocket API on Docker

A detailed guide to building a real-time stock monitoring system using Grafana and WebSocket API on Docker, ideal for tech investors needing a self-hosted, flexible, and scalable...

DPTCloud Engineering4 min read
Build a Real-Time Stock Monitoring System with Grafana and WebSocket API on Docker

Image credit: ElasticComputeFarm · Pixabay Content License

Introduction

In the digital age, tech-savvy investors need a real-time, flexible, and self-hosted stock monitoring system. With Grafana and WebSocket API on Docker, you can build a comprehensive, scalable, and extensible solution that integrates AI for market data analysis. This guide provides step-by-step instructions to deploy a real-time stock monitoring system, suitable for individuals, small businesses, or technology investment organizations.

Requirements and Preparation

To deploy the system, you will need:

  • A server or VPS with at least 4GB RAM (GPU is recommended)
  • Docker and Docker Compose installed
  • Access to a real-time stock API (e.g., Alpha Vantage, Yahoo Finance, or a custom data source)
  • Basics understanding of Docker, Grafana, and WebSocket

Setting Up the System with Docker and Grafana

We will use Docker to deploy key components: a WebSocket server to receive stock data, Grafana for data visualization, and a data storage service (Redis or InfluxDB).

1. Create a docker-compose.yml file with the following configuration:

version: '3.8'

services:
  websocket-server:
    image: node:16-alpine
    container_name: stock-websocket-server
    volumes:
      - ./app:/app
    working_dir: /app
    command: node server.js
    ports:
      - "8080:8080"
    restart: unless-stopped

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    volumes:
      - ./grafana/data:/var/lib/grafana
    restart: unless-stopped

  influxdb:
    image: influxdb:latest
    container_name: influxdb
    ports:
      - "8086:8086"
    volumes:
      - ./influxdb/data:/var/lib/influxdb
    restart: unless-stopped

2. Create a folder named app and create a server.js file to receive stock data via WebSocket:

const WebSocket = require('ws');
const ws = new WebSocket.Server({ port: 8080 });

ws.on('connection', (socket) => {
  console.log('Client connected');
  socket.send(JSON.stringify({ message: 'Connected to stock data feed' }));

  // Send sample data (replace with real API)
  setInterval(() => {
    const stockData = {
      symbol: 'AAPL',
      price: (Math.random() * 100).toFixed(2),
      timestamp: new Date().toISOString()
    };
    socket.send(JSON.stringify(stockData));
  }, 1000);
});

3. Start the system using the command:

docker-compose up -d

Integrating Grafana with Real-Time Data

1. Access Grafana at http://localhost:3000 (default login: admin/admin)

2. Add an InfluxDB data source:

  • Click "Add data source"
  • Select "InfluxDB"
  • Enter URL: http://influxdb:8086
  • Select "InfluxDB 2.x"
  • Enter a name and save

3. Create a new dashboard:

  • Click "Create" → "Dashboard"
  • Add a panel and select "InfluxDB" as the source
  • Select the data table from InfluxDB, e.g., stock_data
  • Set the chart to time-based, value: price
  • Run and view real-time data

Scalable Solutions and AI Integration

To enhance analysis capabilities, you can integrate AI to:

  • Automatically detect price trends
  • Analyze market sentiment from news
  • Send alerts when prices exceed thresholds

Related guides:

Testing and Verification

To verify the system is working:

  1. Run docker-compose ps to check if all containers are running
  2. Access http://localhost:3000 and verify the Grafana dashboard
  3. Test WebSocket connection using a tool like wscat:
wscat -c ws://localhost:8080

Troubleshooting

  • No data on Grafana? Check the connection between InfluxDB and Grafana, ensure the data table exists.
  • WebSocket not connecting? Verify port 8080 is open and the websocket-server container is running.
  • Low performance? Increase RAM or use a VPS with GPU if AI processing is required.

Conclusion

A real-time stock monitoring system using Grafana and WebSocket API on Docker is a comprehensive, self-hosted, flexible, and scalable solution for tech investors. With this architecture, you can integrate AI, analyze data, and make fast, accurate investment decisions.

For effective deployment, see the guide on deploying AI Agents on VPS: Deploy AI Agent on VPS: Detailed A-Z Guide with Docker and GPU.

Related infrastructure & engineering guides: High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478