googleads
Real-Time Apps with TypeScript: Integrating Web Sockets, Node & Angular
preloder
Web Development

Real-Time Apps with TypeScript: Integrating Web Sockets, Node & Angular

Nowadays real-time functionality is a mandatory functionality for web applications. Most of the customers want to see how users interact with their applications in real-time. So I thought to develop a sample application to look at the working of real-time application. In this tutorial, we are going to work together to build a real-time application with Angular 8, Node and WebSockets. 

Here we are going to develop a notification application to notify the admin of the comments of the page. In simple words, the admin will get a notification when the user enters a comment.  

What is Real-time Application?

A real-time application allows information to be received on the user-end soon as soon as it is published, User is directly notified about the new update. It makes it easier for the user to receive the updates directly, instead of putting an effort to search for what he needs. Therefore, this kind of application will increase interactivity between the user and the application. 

What is WebSocket?

​​WebSocket is a protocol for creating a fast two-way channel between a web browser and a server. WebSocket overcomes limitations with HTTP to allow low latency communications between a user and a web service.

Websockets

Project Setup

WebSockets widely supported by more platforms but for this sample, I’m using Angular 8 for client APP and Node.js for the server. Why Angular? It is because Angular is the most powerful front-end framework and we have got huge support. You can also use whichever front-end and server framework if you are able to find necessary plugins for WebSockets. We will be currently working on this environment, 

  1. Node.js 10.16.3
  2. Angular 8.3.19
  3. Npm 6.9.0

Let’s dig into the code now.

Server Code

Since we are going to use WebSocket you can find some WebSocket Sample here. To create a WebSocket environment you need to run the below comments

npm init will fire you some questions like below,

After answering all the above questions system will generate a package.json file inside your application folder.

npm i express socket.io @types/socket.io –save will install the dependencies we need for run WebSocket. Now the environments ready to start server code. 

Now create an app.js inside the src folder to hold our server code.

At the top, we need to require the dependencies plugins.

require statements will tell the system we’re going to use Express and Socket.IO. Socket.IO is coming with some nice features like fallback mechanism, It had the ability to create “rooms” for older browsers that do not support WebSockets and we can see the response in minutes.

In our next line, we are going to allocate memory to store our feedback.

Now let’s define what we want our socket server to do,

Let’s dig deep, .on(‘…’) is an event listener in which we can use predefined events or we can create our own events. For, on the first parameter is events name and second is a callback function. we can tell what needs to perform when the event fires.

The first line .on(‘connection’) is used to find the use connection when the user connects. We will get the socket variable. With the socket variable, we can initiate communication with single or multiple sockets. (connection is predefined event type available in Socket.IO)

Here we have two user-defined event types that our socket is listening for from the client.

  1. addComment
  2. typingComment

And, two event types that are emitted by our socket to the client.

  1. comments
  2. typing

When the client emits the addComment event with comment object, we will push the new comment to comments object and we want to announce everyone who is connected to our server that new comment added to the page. So we are broadcasting the message to all clients with io.emit(‘comments’) so all connected clients will get updated immediately.

And the io.emit(‘typing’) let know the client that somebody started typing. 

After the socket functions are all set up, pick a port and listen to it.

We now have a fully-functioning socket server for document collaboration! Run $ node src/app.js to start it.

Angular Client APP

Now the server is ready to communicate. Let’s start to build the client.

Add depending modules in app.module.ts in before @NgModule declaration, add these lines of code to it.

Now add it to your imports array, so it looks like:

SocketIoModule.forRoot(config) this will fire off the connection to our socket server as soon as AppModule loads.

Comment service

To handle the socket connection and handle comments event.

models/comments.js

Add the following definition to services/comment.service.ts file.

The methods here represent each emits. The two event types that the socket server is listening for, and the properties typing and comments represent the events emitted by the socket server, which is consumed on the client as an Observable, so we can do a lot of cool things with them if we wanted.

You may notice a call to this.commentId(). This is a little private method I created in this service that generates a random string to assign as the comment id.

Let’s put the list of comments under the page content to display the comments in the client and create a form to get the comment from the client. In document-list.component.html, write the following

In comment-list.component.ts, add the following in the class definition,

Let’s start with the properties. comments will be a stream of all available comments emit by a socket. typing is a user socket variable that will get the typing status of the user. onSubmit function will get the values from the input and using the comment.service function newComment will push data to addComment event. comments and typing will act as an Observable and will emit and change the UI once data changes.

Now to loading a comment-list on the home page just replace the app.component with below.

We are done with our coding part let start run our front-end to see the output.

Open more than one instance of http://localhost:4200 (I’ve done it here in separate browsers for added wow factor) and watch it in action.

Find the full source code HERE

Share your views and doubts in the comment section below.

Like this tutorial? Take a look at our blog for more such interesting things you shouldn’t miss. Subscribe for the exclusive newsletters from Agira.

Are you looking for a perfect Angular developer for your app project? Agira Technologies is a Leading Web and mobile development company. Hire an Angular developer now!

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
The following two tabs change content below.

Manikandan Thangaraj

A Passionate full stack developer with 9 years of experience in full stack web application development and expertise in technologies like PHP and Angular. He has huge craze in learning new things about technologies and constantly shares his knowledge with others.

Leave a Reply

Your email address will not be published. Required fields are marked *