Back to search

Build a realtime table with DataTables and Nest.js

  • Christian Nwamba
May 2nd, 2018
A basic understanding of TypeScript and Node will help you with this tutorial. You need to already have Node and npm installed on your machine.

If you have been around the web for sometime now, you will agree with me that extensive data from the database can mostly be perfectly rendered to users by using tables. This is where tools like DataTables really shines, when initialized, it will immediately add more features like searching, ordering and paging to tables.

It drives further than just a preview of data by users. It also brings about an excellent user experience when you add a realtime functionality in order to make changes available to consumers in realtime.

This tutorial will guide you through the process of implementing a realtime feature in tables. The knowledge gained here, will amongst other things help you implement realtime functionality in any web application. Pusher provides awesome tools to make implementing realtime functionality easy on any platform.

The backend of the application will be powered by Nest.js. A progressive Node.js framework for building efficient and scalable server-side applications, Nest.js leverages TypeScript to create reliable and well structured server-side applications.

A quick look at what we'll be building:

realtime-table-nest-demo

Prerequisites

A basic understanding of TypeScript and Node.js will help you get the best out of this tutorial. It is assumed that you already have Node and npm installed. Kindly check Node.js and npm for further instructions and installation steps.

Setting up the application

As at the time of writing, there was no CLI to setup a Nest.js application. The simplest and most efficient way is to clone the starter repository made available on their website.

Now let’s run a command that will create a new project folder named realtime-table-nest-pusher on your machine. Open your terminal or command prompt and run this command :

    $ git clone https://github.com/nestjs/typescript-starter.git realtime-table-nest-pusher

Go ahead and change directory into the newly created folder and install all the dependencies for the project.

    // change directory
    cd realtime-table-nest-pusher

    // install dependencies
    npm install

See it working

Run the application with :

    npm start

The command above will start the application on the default port used by Nest.js. Open your browser and navigate to [http://localhost:3000]. You should see a welcome message like what we have below

realtime-table-nest-hello-world

Server dependencies

Run the command below to install the server dependencies required for this project.

    npm install ejs body-parser pusher
  • ejs: this is a simple templating language for generating HTML markup with plain JavaScript.
  • Body-parser: a middleware used for extracting the entire body portion of an incoming request stream and expose it on req.body .
  • Pusher: a Node.js client to interact with the Pusher REST API

Pusher account setup

Head over to Pusher and sign up for a free account. This is important as it is required before you can have access to an API key and easily access all the awesome features offered by Pusher.

realtime-table-nest-create-account

Create a new app by selecting Channels apps on the sidebar and clicking Create Channels app button on the bottom of the sidebar:

realtime-table-nest-dashboard

Configure an app by providing basic information requested in the form presented. You can also choose the environment you intend to integrate Pusher with for a better setup experience:

realtime-table-nest-create-app

You can retrieve your keys from the App Keys tab:

realtime-table-nest-keys

Bootstrap application

Under the hood, Nest uses the Express library and therefore, favors the popular MVC pattern.

To set this up, open up main.ts file and update it with the content below:

    // ./src/main.ts

    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';

    import * as bodyParser from 'body-parser';
    import * as express from 'express';
    import * as path from 'path';

    async function bootstrap() {
      const app = await NestFactory.create(AppModule);
      app.use(express.static(path.join(__dirname, 'public')));
       app.set('views', __dirname + '/views');
       // set ejs as the view engine
       app.set('view engine', 'ejs');
      await app.listen(3000);
    }
    bootstrap();

This is the entry point of the application and necessary for bootstrapping Nest.js apps. I have included the Express module, path and set up ejs as the view engine for the application.

Datatables setup

Generally, getting DataTable running on any website or app require little or less configuration as it is easy to set up and quite straight forward. All that is required is to include the necessary links on our HTML page.

For this we will create a folder called views within the src folder. Now go ahead and create a new file named index.ejs within it. In the newly created file, paste in the following code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
        <link rel="stylesheet" href="/style.css">
        <title> Datatable Realtime </title>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <h2 class="text-center">
                    Realtime Data Table
                </h2>
                <div class="col-md-7">
                    <table id="realtime" class="display" width="100%"></table>
                </div>

                <div class="col-md-4 col-md-offset-1">
                    <h3 class="text-center">Create New Employee</h3>

                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" name="name" id="name" placeholder="Name" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="position">Position</label>
                        <select name="position" id="position" class="form-control">
                            <option value="">--Select Position--</option>
                            <option value="Frontend Developer">Frontend Developer</option>
                            <option value="UI/UX Engineer">UI/UX Engineer</option>
                            <option value="iOS Engineer">iOS Engineer</option>
                            <option value="Android Developer">Android Developer</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="office">Office</label>
                        <select name="office" id="office" class="form-control">
                            <option value="">--Select Office--</option>
                            <option value="Lagos">Lagos</option>
                            <option value="London">London</option>
                            <option value="New York">New York</option>
                            <option value="Berlin">Berlin</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="extn">Extn</label>
                        <input type="number" name="extn" id="extn" placeholder="Extn" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="startDate">Start Date</label>
                        <input type="date" name="startDate" id="startDate" placeholder="Start Date" class="form-control">
                    </div>

                    <div class="form-group">
                        <button class="btn btn-info" id="add-employee">Add Employee</button>
                    </div>
                </div>
            </div>
        </div>

        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.16.2/axios.js"></script>
        <script src="https://js.pusher.com/4.1/pusher.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/plug-ins/1.10.15/api/row().show().js"></script>
        <script src="/data.js"></script>
        <script src="/main.js"></script>
    </body>
    </html>

This will serve as the homepage for this application. In order to make this page look presentable, we included a CDN CSS file each for Bootstrap and DataTable. Further, we’ve included a custom CSS file named style.css. You can download it here from the repository. Once you are done, create a new folder named public within the src folder and save the stylesheet file in it.

In addition, we included a table element identified by an id of #realtime, this element will hold the DataTable. We have also defined relevant input fields that will be used to add values to a dataset that we’ll revisit later in the tutorial.

Also included is a CDN file each for jQuery, Axios, Pusher, and DataTable. And finally, just before the closing tag of the <body> element on the page, we’ve included two script file named data.js and main.js. data.js will contain sample dataset that will be used to initialize our DataTable, while main.js will hold the custom script for the application. Now, go ahead and create this two files and save them in the public folder as we’ll need them later in the tutorial.

Initialize DataTable

Earlier, we inserted a table element with an id #realtime on our page. In order to initialize the DataTable and create a blueprint for our table, we’ll use jQuery to append the DataTable method to the element and passed an object as argument. The object has a data property which takes a dataset . You can download the dataset here and save the content in the data.js that was created earlier. We also include another property called columns and set its value to an array of objects with each object’s value serving as a column header for our table.

    //   ./src/public/main.js


    const app = {
        ...
        start() {
          const dataTable = $('#realtime').DataTable({
            data: dataSet,
            columns: [
              { title: 'Name' },
              { title: 'Position' },
              { title: 'Office' },
              { title: 'Extn.' },
              { title: 'Start date' }
            ]
          });
          ...
        }
      };

      $(document).ready(() => app.start());

Handling routes

The controller layer in Nest.js is responsible for receiving an incoming request and returning the appropriate response to the client. Nest uses a controller metadata @Controller to map routes to a specific controller. The starter project already contains a controller by default. We will make use of this in order to render the home for this app. Open ./src/app.controller.ts and edit as shown below:

    // ./src/app.controller.ts

    import { Get, Controller, Res } from '@nestjs/common';

    @Controller()
    export class AppController {
      @Get()
      root(@Res() res) {
        res.render('index');
      }
    }

This controller will ensure that Nest maps every / route to index.ejs file.

Adding new records to the table

We’ve already included the relevant input fields required to add more records to the dataset in the DataTable. To make this work, we’ll use a custom function called buildForm() to retrieve all the values of the input fields. Open ./src/public/main.js and add the function

    const app = {
        buildForm() {
          return [
            $('#name').val(),
            $('#position').val(),
            $('#office').val(),
            $('#extn').val(),
            $('#startDate').val().replace(new RegExp('-', 'g'), '/')
          ];
        },

        start() {
        ...
        }
      };

      $(document).ready(() => app.start());

In addition we created two more methods processForm() and addRow() . While the former is responsible for processing and passing the formData returned by buildForm() to the server, the latter takes in two arguments and handles the addition of realtime data to DataTable.

    // ./src/public/main.js

    const app = {
        buildForm() {
          ...
        },

        processForm() {
          const formData = this.buildForm();
          const baseURL = 'http://localhost:3000';
          axios.post(`${baseURL}/record`, formData)
            .then(response => console.log(response));
        },

        start() {
          ...
        }
      };

      $(document).ready(() => app.start());

Passed the formData to the server, we will set this up in a bit:

    // ./src/public/main.js


    const app = {
        buildForm() {
         ...
        },

        processForm() {
          ...
        },

        addRow(dataTable, data) {
          const addedRow = dataTable.row.add(data).draw();
          addedRow.show().draw(false);

          const addedRowNode = addedRow.node();
          $(addedRowNode).addClass('highlight');
        },

        start() {
        ...      
        }
      };

      $(document).ready(() => app.start());

As stated earlier, this method takes in dataTable instance and the newly added data as arguments.

The methods row.add() and .draw() are inbuilt DataTables API methods, other DataTables methods implemented in addRow() are .show(), .draw(false) and .node():

  • row.add() adds a new row to the table using the given data.
  • .draw() redraws and updates the table in the current context.
  • .show() displays a field in our table. This is useful for cases when you want to have extra form fields available, but only show them under certain conditions.
  • .draw(false) adds a new row without resetting or distorting the current page.
  • .node() serves as an event listener, it returns the DOM element for the requested field thus enabling DOM manipulation of the field.

We then take our processForm() method which we built and bind it to a button using jQuery’s .click() method. When the button is clicked, addRow() automatically executes its functions on our table.

    const app = {
        buildForm() {
          ...
        },

        processForm() {
          ...
        },

        addRow(dataTable, data) {
         ...
        },

        start() {
          ...
          });

          // bind the processForm() method to a button
          $('#add-employee').on('click', this.processForm.bind(this));
        }
      };

      $(document).ready(() => app.start());

Find the complete custom script here.

Create a controller

Earlier, we already configure the app.controller.ts to render the homepage and display the form for consumers. The next thing we need to do is build the controller that will handle the data posted to the server. Create a new folder named table in the src folder and create a new file called table.controller.ts within it.

    import { Controller, Post, Res, Body, HttpStatus } from '@nestjs/common';
    import { TableService } from './table.service';


    @Controller('record')
    export class TableController {
        constructor(private tableService:TableService){}

        @Post()
        addNewRecord(@Res() res, @Body() data: String) {
            this.tableService.add(data);
            res.status(HttpStatus.OK).send('Pushed');
        }

    }

One of the most important modules imported here is the TableService . It was injected into the controller through the constructor. As recommended by Nest a controller should handle only HTTP requests and abstract any complex logic to a service.

Realtime service with Pusher

As required within the TableController above, lets create a component as a service. This will basically receive the formData and publish it to a designated channel for the client side to listen and subscribe to. So create a new file within table folder named table.service.ts

    // ./src/table/table.service.ts

    import { Component } from '@nestjs/common';

    @Component()
    export class TableService {
        add(newEmployee) {
            const Pusher = require('pusher');

            var pusher = new Pusher({
                appId: 'YOUR_APP_ID',
                key: 'YOUR_API_KEY',
                secret: 'YOUR_SECRET_KEY',
                cluster: 'YOUR_CLUSTER',
                encrypted: true
              });

              pusher.trigger('employees', 'new-employee', newEmployee);
        }
    }

Here we have initialized Pusher with the required credentials in order to be able to trigger an event named new-employee through a channel named employees.

Connecting the dots

To make this work, both the TableController and TableService needs to be registered within the app.module.ts file.

    // ./src/app.module.ts

    import { TableService } from './table/table.service';
    import { TableController } from './table/table.controller';
    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';

    @Module({
      imports: [],
      controllers: [AppController, TableController], // add controller
      components: [TableService], // add service
    })
    export class AppModule {}

Updating the UI

To update the table once a form is submitted, in our main.js file (client) we used the subscribe() method from Pusher to subscribe to the created employees channel.

    const app = {
        buildForm() {
          ...
        },

        processForm() {
         ...
        },

        addRow(dataTable, data) {
          ...
        },

        start() {
          ...
          // subscribe to a channel
          var pusher = new Pusher('YOUR_API_KEY', {
            cluster: 'CLUSTER',
            encrypted: true
          });

          var channel = pusher.subscribe('employees');
          channel.bind('new-employee', (data) => {
            this.addRow(dataTable, data);
          });
        }
      };

      $(document).ready(() => app.start());

Bringing it all together

Restart the development server if it is currently running. Check your page on [http://localhost:3000.](http://localhost:3000.)

realtime-table-nest-demo

Conclusion

So far, we have learnt how to leverage on the realtime functionality provided by Pusher to add more records to existing data. Feel free to explore more by visiting Pusher’s documentation. And lastly, the complete source code of this demo application can be found here on github.

  • Channels

© 2018 Pusher Ltd. All rights reserved.

Pusher Limited is a company registered in England and Wales (No. 07489873) whose registered office is at 28 Scrutton Street, London EC2A 4RP.