DEV Community

Cover image for Part 1/3 - How to create a server-side timer using WebSockets (with Socket.IO), NestJS and Flutter
Rukshan J. Senanayaka
Rukshan J. Senanayaka

Posted on • Updated on

Part 1/3 - How to create a server-side timer using WebSockets (with Socket.IO), NestJS and Flutter

If you have an interest in WebSockets technology, chances are that you have seen several tutorials online that use it to create applications such as realtime chat apps. But is that all that is possible with this WebSockets technology πŸ€”? - Of course not! We can do so much more with them.

In this 3-part tutorial series, we will learn how to create a server-side timer using NestJS and Flutter by application of WebSockets (with Socket.IO). You can use such an implementation to create a proper timer for a quiz app etc.

Pre-requisites

  • A decent knowledge on NestJS, Flutter, JavaScript/TypeScript.
  • Flutter 2.5.2 (with null safety), NestJS 8.0.0, Node 16.15.0, Android Emulator API S (This series of articles is successfully tested with these - may or may not work with other versions)

Final outcome of this 3-part article series

Image description

Introduction

This part (part 1/3) contains the basic theory and some other good-to-know info related to WebSocket communication.

In the world of connectivity, users need to send and receive data to and from various locations around the world. REST APIs are used widely to achieve this purpose. But in the traditional client-server communication using REST API and HTTP protocol, the server only responds to a request firstly made by the client - not initiate a data transfer by itself.

Therefore, to get new data, a client has to request new data each time it needs that updated data. But how does a client know whether it's the right time to request new data or to wait for some time until the data is updated in the server?

A few methods are available to achieve this,

  • Refresh the client to send a new HTTP request every time
  • Polling
  • WebSockets

Out of these, refreshing the client repeatedly is not good user experience. It is also not sometimes practical.

Polling is expensive in terms of computational power and resources required.

WebSockets is a high speed technology that can be used for realtime client-server communication.

The socket confusion

First off, let's clarify a couple of things.

There are a couple of jargon words used in socket communication (mostly interchangeably) in general, but it would be nice to have a clear idea about them. I'll just give a brief of some such words below and you can refer the references for more information on them.

  • WebSocket - is a protocol that depends on TCP protocol (like HTTP)
  • WebSockets - is the API specification of WebSocket protocol (supports two way communication)
  • Socket.IO - is a library (built on top of WebSocket protocol)
  • Socket - is an endpoint (in a socket communication)
  • Webhook - is an HTTP-based callback function which allows one way, server to server communication (not related to WebSockets and not concerned in this tutorial series)

Although Sockets can be divided as

  • TCP/IP sockets
  • Unix domain sockets,

we are only interested in TCP/IP sockets for this tutorial series.

Steps in establishing a WebSocket connection

  1. Client sends an HTTP request to open a WebSocket connection (the HTTP connection will be upgraded to a WebSocket connection)

  2. Server responds to the client’s request approving it with a 101 Switching Protocols response, and thereby completing the handshake and establishing a WebSocket connection.

  3. This TCP/IP connection is kept open (by a ping-pong mechanism) allowing real time communication between the client and server until either the client or the server chooses to close the connection. This is a full-duplex communication, meaning both the client and server can send messages to each other at the same time.

  4. After either side terminates the connection, the TCP resources would be unallocated.

Image description

In traditional client-server communication, a client sends a request to a server to retrieve some kind of a resource (a text, an image or an audio etc.) and waits for a response from the server. But in WebSocket communication, the server can send the resource when required - without a direct request from a client.

With the successful establishing of a handshake, a communication channel is created between client and server and then whenever the server wants to send something, it just can send it to the particular client without needing a REST API request from the client itself.

Either endpoint of this communication channel is called a Socket. Therefore, a socket has an IP address and a port number.

Image description

In this 3-part article series, we would be implementing a server-side timer using NestJS and Flutter, and we can start and stop it using our Flutter application.

Disclaimer

I'm also learning this stuff and there can be better ways to do the things that I have done in this tutorial series. Please feel free to give your feedback in the comments. If I have made a mistake, I'm more than grateful if you can bring them up in the comments section in each article πŸ€“

Conclusion

Sockets are a great way to implement realtime functionalities in applications. For example, WebSockets can be used to implement a server-side timer for a Flutter app.

This way, the clients have no way to tamper with or manipulate the timer on the frontend, and therefore this can be used, for example in quiz apps to maintain a server-side timer for each question with high integrity.

Now you can go to the part 2 of this tutorial series, to see how to implement the backend server application required for WebSocket communication, in NestJS.

Video

IMAGE ALT TEXT HERE

References

  1. General concept of a socket - https://medium.com/swlh/understanding-socket-connections-in-computer-networking-bac304812b5c

  2. WebSocket Protocol - https://datatracker.ietf.org/doc/id/draft-ietf-hybi-thewebsocketprotocol-09.html

  3. WebSocket API (WebSockets) - https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

  4. Socket.IO can be more than just a wrapper for WebSockets - https://en.wikipedia.org/wiki/Socket.IO

  5. WebSockets v Socket.IO - https://stackoverflow.com/a/62848079/8995555

  6. WebSocket v Socket - https://stackoverflow.com/a/67826460/8995555

  7. WebSockets v Webhooks - https://stackoverflow.com/a/24747947/8995555

  8. Unix Socket v TCP/IP Socket (Just for additional knowledge) - https://www.baeldung.com/linux/unix-vs-tcp-ip-sockets

  9. What is a Webhook - https://www.redhat.com/en/topics/automation/what-is-a-webhook

  10. HTTP to WS upgrade request - https://stackoverflow.com/a/26401940/8995555

Top comments (4)

Collapse
 
nipunaniranjan profile image
NipunaNiranjan

Very informative , need more articles!!!

Collapse
 
rukshanjs profile image
Rukshan J. Senanayaka

Thank you πŸ˜ƒ! Will bring more for sure!

Collapse
 
stharana profile image
Sathyanga Tharana

Nice Article <3

Collapse
 
rukshanjs profile image
Rukshan J. Senanayaka

Thank you πŸ˜ƒ