Track MySQL Database Changes With WebSockets Using PieSocket

Learn how to track MySQL database changes with PHP, Laravel, Django, SpringBoot, and almost any other Framework or language.

We know that WebSockets can help us build real-time applications. If you are not familiar with WebSockets yet, I suggest you read our guide What is WebSocket, it covers a brief introduction of WebSockets and their use cases.

Today we are going to talk about one of many such use-cases and help you track your MySQL data in real-time. We will use PieSocket WebSocket Channels in this example because PieSocket provides the needed libraries to make this possible in few simple lines of code.

There are two parts of this solution to track the MySQL database in real-time and push the changes immediately to the end-user as they happen.

We are going to use the following example to demonstrate how to track MySQL changes in real-time with WebSockets.

Let’s say there is a table named users in your MySQL database and you have a page that lists all the users in a HTML table. Our goal is to update the user’s list on this page immediately as soon as a new user entry is made in the users table.

Examples in this tutorial use PHP, however the same can be implemented using any programming language. You will not have difficulties understanding the example code even if you are from Python, Node.js, or Go background.

We have added examples for the following back-end frameworks in the sections below:

Setup Frontend

Let’s start by creating a file that lists all existing users in an HTML table, we will call this file index.php for simplicity.

File: index.php

The file above displays current users in the users table but does not update automatically when a new user is added to MySQL on the backend. That is where WebSockets come in. To save ourselves the hassles of creating and hosting a WebSocket server, we will use the managed WebSocket Channels from PieSocket.

So start by creating a PieSocket API key from here. PieSocket has a free plan which should be enough for small projects.

Next, add the PieSocket-JS WebSocket library in your HTML file with the following code:

Now, we are ready to connect to the WebSocket channel and start receiving instantaneous updates from the server. Add the following code to your index.php file to make a websocket connection.

This is all for the frontend side. This code shows an alert when a new database entry is available, we leave it up to you to append the incoming data to the existing HTML table. Now it is time to push events from the backend when a new entry is available.

Setup Backend

Let’s create a file called admin.php which adds an entry to the user’s table, every time it is visited.

The code snippet given above adds an entry in the users table every time its execute either via CLI or from a webserver.

Our frontend table still stays the same, the changes are visible only after a page refresh.
To fix that, we need to make a REST API call to PieSocket server every time we modify the users table.

Use the following PHP function to do that.

This is it, call the function publishUser every time you modify your users table and the frontend will show an alert with the user payload information. You can automate this process with help of Observers if you are using frameworks like Laravel, Django, Ruby on rails, SpringBoot, etc.

Track MySQL changes in real-time with Laravel

The backend setup part as mentioned above becomes even easier while using a framework like Laravel. You can add publishUser call in a Model observer and Laravel will publish the event to the WebSocket channel automatically when a Model is created, updated, or deleted.

For example, in your User model under app/model/User.php add the following code block

Track MySQL changes in real-time with Django

Django supports signals which can be used to publish events on the frontend automatically when a new record is created in the database.

Track MySQL changes in real-time with Node.js

It is even easier to publish database change events using Node.

First, install the piesocket-node library with the following command:

Then, use the code snippet below to send an event.

Alternatively, you can make a CURL request from your node service, see the documentation: here.

Track MySQL changes in real-time with Ruby On Rails

Use ActiveRecord::Observer with Ruby on rails to publish an event on the frontend automatically when a model is created or updated. Following is a Ruby code snippet you can use.

Track MySQL changes in real-time with SpringBoot

Use the following class to publish events when a Model changes in your SpringBoot application. This tutorial explains in-depth how to implement Observer patterns in SpringBoot to automate this process.

Visit PieSocket Documentation to learn about to use the service with various programming languages and frameworks. Hope this helps!