Sling Academy
Home/Golang/Building a Stock Price Tracker Using WebSockets in Go

Building a Stock Price Tracker Using WebSockets in Go

Last updated: November 26, 2024

In this article, you will learn how to build a real-time stock price tracker using WebSockets in Go. Real-time updates are crucial in many applications; using WebSockets allows our applications to receive live updates from a server efficiently.

Prerequisites

  • Basic understanding of Go (Golang)
  • Go installed on your machine
  • Familiarity with terminal commands
  • A code editor such as VS Code

Setting Up Your Project

Firstly, create a new directory for our stock tracker project and navigate into it:

mkdir stock-tracker
cd stock-tracker

Next, initialize a new Go module:

go mod init stock-tracker

Installing Dependencies

For WebSockets, we will use the popular "gorilla/websocket" package. Install it by running:

go get -u github.com/gorilla/websocket

Creating the WebSocket Server

Let’s start by creating a server that will push stock updates:

Create a file named main.go and insert the following code:

package main

import (
    "fmt"
    "log"
    "net/http"
    "github.com/gorilla/websocket"
    "math/rand"
    "time"
)

var upgrader = websocket.Upgrader{
    CheckOrigin: func(r *http.Request) bool {
        return true // Allow all connections
    },
}

func main() {
    http.HandleFunc("/ws", handleConnections)
    go simulateStockUpdates()
    log.Println("[Server] HTTP server started and listening on port :8080")
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

var clients = make(map[*websocket.Conn]bool)
var broadcast = make(chan StockPrice)

type StockPrice struct {
    Symbol string  `json:"symbol"`
    Price  float64 `json:"price"`
}

func handleConnections(w http.ResponseWriter, r *http.Request) {
    ws, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Fatal(err)
    }
    defer ws.Close()
    clients[ws] = true
    log.Printf("[Server] New WebSocket connection: %s", ws.RemoteAddr())

    for {
        var msg StockPrice
        err := ws.ReadJSON(&msg)
        if err != nil {
            log.Printf("[Server] Error: %v", err)
            delete(clients, ws)
            break
        }
    }
}

func handleMessages() {
    for {
        msg := <-broadcast
        for client := range clients {
            err := client.WriteJSON(msg)
            if err != nil {
                log.Printf("[Server] Error: %v", err)
                client.Close()
                delete(clients, client)
            }
        }
    }
}

func simulateStockUpdates() {
    symbols := []string{"GOOG", "AAPL", "AMZN", "MSFT", "TSLA"}
    rand.Seed(time.Now().UnixNano())
    for {
        symbol := symbols[rand.Intn(len(symbols))]
        price := rand.Float64()*1000 + 100
        broadcast <- StockPrice{Symbol: symbol, Price: price}
        time.Sleep(time.Second * 5) // Send update every 5 seconds
    }
}

Setting Up WebSocket Client

Now, let's create a simple client to connect to our WebSocket server. Create a new file called client.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stock Price Tracker</title>
</head>
<body>
    <h1>Live Stock Price Tracker</h1>
    <ul id="stockList"></ul>
    <script>
        const ws = new WebSocket('ws://localhost:8080/ws');

        ws.onmessage = function(event) {
            const stock = JSON.parse(event.data);
            const stockList = document.getElementById('stockList');
            const newStockInfo = `<li>${stock.symbol}: $${stock.price.toFixed(2)}</li>`;
            stockList.innerHTML += newStockInfo;
        };
    </script>
</body>
</html>

Running the Application

To see your live stock price tracker in action:

  1. Run your Go application by executing in the terminal: go run main.go.
  2. Open the client.html file in a web browser to see live updates of stock prices.

Congratulations! You've successfully built a simple stock price tracker using WebSockets in Go.

Next Article: Integrating WebSockets with Go and React for Real-Time UIs

Previous Article: Handling WebSocket Errors and Reconnection Strategies in Go

Series: Websocket & Chat Programs in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant