Using Socket.IO with NodeJS on NVIDIA Jetson Nano / Jetson TX2 NX / Jetson Xavier NX Modules - Forecr.io

Using Socket.IO with NodeJS on NVIDIA Jetson Nano / Jetson TX2 NX / Jetson Xavier NX Modules

Jetson Nano | Jetson TX2 NX | Jetson Xavier NX

01 June 2021
WHAT YOU WILL LEARN?

1- Introduction to the Socket.IO

2- Socket.IO in a nutshell

3- How to create a new NodeJS application using ExpressJS

4- How to install socket.IO library on NodeJS applications

5- How to communicate between server-side and client-side using Socket.IO

ENVIRONMENT

Hardware: DSBOARD-NX2

OS: JetPack 4.5 ( L4T - 32.5)

Language: JavaScript



Introduction to the Socket.IO


In this article, we will show you how to use Socket.IO messaging library in NodeJS on ARM64 which has an operating system as Ubuntu 18.04 for NVIDIA Jetson Nano & Xavier NX & TX2 NX Modules.


We will use our carrier board, Forecr DSBOARD-NX2 with NVIDIA Jetson Xavier NX module. You can find detailed information about DSBOARD-NX2 from the link below.


https://www.forecr.io/products/dsboard-nx2


Socket.IO in a Nutshell


Socket.IO is a JavaScript library for real-time web applications. It enables real-time, bi-directional communication between web clients and servers. It has two parts: a client-side library that runs in the browser, and a server-side library for the server-side. In this application, we will communicate with the website using Socket.IO on the NodeJS server side.


NodeJS on Socket.IO


First of all, NodeJS must be installed on our computer to use socket.IO. “nodejs -v” with that command, we can see which version we have installed, and also, we can verify NodeJS installed.


If NodeJS is not installed, you can learn how to install it from our article on the link below.


https://www.forecr.io/blogs/programming/get-started-with-nodejs-on-nvidia-jetson-nano-xavier-nx-modules-run-basic-web-app


After verifying our NodeJS version, let’s run a sample Socket.IO sample using a NodeJS. First, create a folder named socketIOSample.



Now, navigate to the project directory with the following command:



How to create a new NodeJS application using ExpressJS


We will create our NodeJS web application with the express.js library. ExpressJS is an open-source back-end web application framework for Node.js. We can design easily for building web applications and API’s.


Let's create our application with express.js by entering the following commands.



Use the npm init command to create a package.json file for our application.

We enter index.js our name of the main file. 

Now install express in the socketIOSample directory and save it in the dependencies list with following command:



How to install Socket.IO library on NodeJS applications


Now, we need to install Socket.IO library. To do this, enter the following commands:



After all library installed. We have 2 files; server side named index.js and the other one is client-side named index.html.


Server-side sample code and containing the following contents:



 Client-side sample code and containing the following contents:


How to communicate between server-side and client-side using Socket.IO


In this application, our main focus is to establish a connection between the client-side and server-side with Socket.IO and show the message we read from the client-side and send it back to the client-side.


With the command below, we connect to the client-side and when the data message is received, we listen to the incoming message and send the same message to the client-side with the emit command.





Now run our basic socket.IO app server using the following command and type:


“http://127.0.0.1:3000“ in any browser, the outputs will be as follow:



Server-side output



Client-side output




Thank you for reading our blog post.