Create a live graph with Go
You will need Go 1.10+ installed on your machine. Basic knowledge of Go and JavaScript will be helpful.
An astronomical number of data streams are being generated every day because data is core to diverse systems. Today’s business owners have come to recognize data as an important factor during decision making.
However, the gathered bulk data would be confusing to read and useless without a way to visualize it. For this reason, realtime monitoring systems have gained popularity over the years. We can use such systems to visualize website traffic, weather updates, server performance and so on.
In this article, we will build a realtime monitoring graph that continuously displays updates on website traffic after a fixed interval. We will build a Go backend server that exposes several endpoints and uses Pusher Channels to trigger events on the count of user visits. We will simulate the website traffic data using a simple setInterval function and a pseudorandom number generator.
On the frontend, we will receive the simulated updates from Pusher and render the data trend using a line chart. The graph will be a plot on “user visits” against “total number of pages visited.” For context, here is a sample of what we will be building:
From the image above, we can see that there is a button labeled Simulate user visits, this button is responsible for starting the simulator on the Backend server. Let’s begin.
Requirements
To follow along with this article, you will need the following:
- An editor like Visual Studio Code.
- Go (version >= 1.10.x) installed on your computer. Heres how you can install Go.
- Basic knowledge of the Go programming language.
- Basic knowledge of JavaScript (ES6).
Once you have all the above requirements, we can proceed.
Setting up Pusher
The first step will be to get a Pusher Channels application. We will need the application credentials for our realtime features to work.
Go to the Pusher website and create an account. After creating an account, you should create a new application. Follow the application creation wizard and then you should be given your application credentials, we will use this later in the article.
Setting up the codebase
Let’s begin by navigating into the src
directory that is located in the $GOPATH
and creating a new directory for our app. This will be our working directory:
$ cd $GOPATH/src
$ mkdir pusher-go-realtime-graph
$ cd pusher-go-realtime-graph
Create a new file called graph.go
. Now, before adding content to this file, we need to install the Go packages that we will be referencing within our application.
Run the following packages to pull in the packages:
$ go get github.com/labstack/echo
$ go get github.com/labstack/echo/middleware
$ go get github.com/pusher/pusher-http-go
⚠️ If you use Windows and you encounter the error ‘cc.exe: sorry, unimplemented: 64-bit mode not compiled in ‘, then you need a Windows gcc port, such as https://sourceforge.net/projects/mingw-w64/. Also, see this GitHub issue.
Within your favorite editor, open the graph.go
file and update it with the following code:
package main
import (
"math/rand"
"net/http"
"time"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
pusher "github.com/pusher/pusher-http-go"
)
// We register the Pusher client
var client = pusher.Client{
AppId: "PUSHER_APP_ID",
Key: "PUSHER_APP_KEY",
Secret: "PUSHER_APP_SECRET",
Cluster: "PUSHER_APP_CLUSTER",
Secure: true,
}
// visitsData is a struct
type visitsData struct {
Pages int
Count int
}
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Define the HTTP routes
e.File("/", "public/index.html")
e.File("/style.css", "public/style.css")
e.File("/app.js", "public/app.js")
e.GET("/simulate", simulate)
// Start server
e.Logger.Fatal(e.Start(":9000"))
}
The graph.go
file is the main entry point of our backend server so this is where we import all the packages we previously pulled in. We import the time
package because we will need it to create a new ticker for our custom setInterval
function. We import the math/rand
package because it provides the pseudorandom number generation.
Next, registered a new Pusher client with the credentials from the app we created earlier.
⚠️ Replace
PUSHER_*
keys with your app credentials.
We also created a visitsData
type, which is a struct with two fields:
Pages
- this is the number of pages visited at a given timeCount
- this is the user count at a given time
We defined the main function (this is where the application starts its execution) and created a new echo instance.
In this application, because we will serve the static files that define our frontend from the backend server, we need to let Go know about them and their location. In the main function, we registered the routes that will serve these static files that we will create shortly.
We also defined a “/simulate” route that takes a simulate
handler function and responds to the GET
HTTP verb. Lastly, we instruct the echo instance we created to start the application and listen on the port 9000.
Let’s create the simulate
function, before the start of the main function paste the following code:
// [...]
// -------------------------------------------------------
// Simulate multiple changes to the visitor count value,
// this way the chart will always update with different
// values.
// -------------------------------------------------------
func simulate(c echo.Context) error {
setInterval(func() {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
newVisitsData := visitsData{
Pages: r1.Intn(100),
Count: r1.Intn(100),
}
client.Trigger("visitorsCount", "addNumber", newVisitsData)
}, 2500, true)
return c.String(http.StatusOK, "Simulation begun")
}
// [...]
Within the simulate
function, we called a setInterval
function and within it we seeded a new random number using the time.Now()
function. We also instantiated a new visitsData
object with the name newVisitsData
and initialized both the Pages
and Count
fields using randomly generated numbers. We set the interval between running the logic to 2500ms.
Lastly, we use the Trigger
method on the client object to broadcast an addNumber
event over the visitorsCount
channel.
Let’s create the setInterval
function before the simulate
function paste the following code:
func setInterval(ourFunc func(), milliseconds int, async bool) chan bool {
// How often to fire the passed in function in milliseconds
interval := time.Duration(milliseconds) * time.Millisecond
// Setup the ticker and the channel to signal
// the ending of the interval
ticker := time.NewTicker(interval)
clear := make(chan bool)
// Put the selection in a go routine so that the for loop is none blocking
go func() {
for {
select {
case <-ticker.C:
if async {
// This won't block
go ourFunc()
} else {
// This will block
ourFunc()
}
case <-clear:
ticker.Stop()
return
}
}
}()
// We return the channel so we can pass in
// a value to it to clear the interval
return clear
}
Using Goroutines and Channels, we have created a setInterval
function that works like the one in JavaScript. For its parameters, it receives the function to be called after an interval, the interval and a boolean value that determines if it runs asynchronously or not.
Building the frontend
In this step, we will use the received data from the backend server to represent a mathematical graph. To achieve this, we can use any of the open source libraries that support chart types, such as bar charts, pie charts, line charts and scatter charts. Here are links to a few of the popular libraries:
In this tutorial, we will use Chart.js; it has a fairly simple API and renders robust charts using the HTML canvas tag. It is also able to update the chart without completely re-rendering it and this is what we need since we will be rendering Pusher updates.
To start building the frontend of our application, we will create a public
folder in our working directory and create three files within it:
$ mkdir public
$ cd public
$ touch index.html style.css app.js
Open the index.html
file and update it with the following code:
<!DOCTYPE>
<html>
<head>
<title>Realtime User Analytics</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:200">
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<section class="subheader">
<h2>Realtime User Traffic Analytics</h2>
<button id="getUpdates" class="btn-primary"> Simulate user visits</button>
</section>
<section width="500px" style="margin:0 auto;width: 100%;max-width: 800px">
<canvas id="realtimeChart"></canvas>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"></script>
<script src="https://js.pusher.com/4.0/pusher.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript" src="./app.js"></script>
</body>
</html>
In the scripts part of the markup above, we pulled in the JavaScript Pusher library, Axios, and Chart.js.
In order to style our app properly, add the following CSS into the style.css
file the public folder:
body{
margin:0;
padding:0;
font-family: Raleway;
}
header{
background: #2b303b;
height: 50px;
width:100%;
display: flex;
color:#fff;
}
.subheader{
margin: 20px;
text-align: center;
}
Displaying realtime graph updates using Pusher
In this section, we will write the vanilla JavaScript code that will receive updates from Pusher and update the graph accordingly. We will achieve this behavior by subscribing to the unique channel where our server will be sending update events for the graph. The name of this channel is visitorsCount
and the event we will be binding to is called addNumber
.
Because we want to control when the realtime updates start, we will use JavaScript to target the button element — simulate user visits — in the markup and attach an eventListener
, so that the updates only start coming in when we click the button. When the button is clicked, our frontend will make a GET
request, using Axios, to the “/simulate” endpoint on our backend server and the realtime updates will start.
We will also instantiate a new line chart object passed in a chartConfig
object as an argument, this chartConfig
object will supply the data that will define the line chart’s properties.
Open the app.js
file and update it with the following code:
document.getElementById("getUpdates").addEventListener("click", function(){
axios.get('/simulate')
})
const pusher = new Pusher('PUSHER_APP_KEY', {
cluster: 'PUSHER_APP_CLUSTER',
encrypted: true
});
const channel = pusher.subscribe('visitorsCount');
channel.bind('addNumber', data => {
if (newLineChart.data.labels.length > 15) {
newLineChart.data.labels.shift();
newLineChart.data.datasets[0].data.shift();
}
newLineChart.data.labels.push(data.Count);
newLineChart.data.datasets[0].data.push(data.Pages);
newLineChart.update();
});
function renderChart(userVisitsData) {
var ctx = document.getElementById("realtimeChart").getContext("2d");
var options = {};
newLineChart = new Chart(ctx, {
type: "line",
data: userVisitsData,
options: options
});
}
var chartConfig = {
labels: [],
datasets: [
{
label: "Realtime User Analytics",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [],
spanGaps: false,
}
]
};
renderChart(chartConfig)
⚠️ Replace
PUSHER_APP_*
keys with your app credentials.
Running the application
We can run our application with this command:
$ go run graph.go
We can now point our web browser to this address http://localhost:9000, at first, the graph in the window will be static but after we click on the Simulate users visit button, we will see this the realtime updates:
Conclusion
In this article, we have discussed the importance of data in today’s tech world and gone over the process of building a realtime monitoring system using Go and Pusher channels. In addition to that, we used Vanilla JavaScript to build the frontend of our application and hit an endpoint on our Go web server using Axios.
The source code to the application is available on GitHub.
29 June 2018
by Neo Ighodaro