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-trackerNext, initialize a new Go module:
go mod init stock-trackerInstalling Dependencies
For WebSockets, we will use the popular "gorilla/websocket" package. Install it by running:
go get -u github.com/gorilla/websocketCreating 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:
- Run your Go application by executing in the terminal:
go run main.go. - Open the
client.htmlfile 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.