A Casual Look Into Options For Building Real-time Apps
Tags: webdev, websockets
Published: 2021-02-04 00:55:08

*[Cover Image source: LogRocket Blog](https://blog.logrocket.com/beyond-rest-using-websockets-for-two-way-communication-in-your-react-app-884eff6655f5/)*
# Intro
Imagine logging onto Facebook with the intention of talking to your sibling. You navigate to the messenger page. You type in your message and press the send button. You wait a moment and then refresh the page. Nothing changes. You wait another moment and press refresh again. Still nothing. You leave your computer and do a load of laundry before returning your computer and clicking refresh. FINALLY. The response from your sister loads with a timestamp from 5 minutes ago. You type in your next message, refresh the page, and the cycle continues.
Clearly, this is not how the modern messenger app works! Having to refresh the page every time you want to check for new messages is extremely inefficient. In order to meet the demands of modern real-time web applications, WebSockets were created. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7o8c03m0x2qn39cw8sup.png)
Image source: *[PI Web API 2017](https://livelibrary.osisoft.com/LiveLibrary/content/en/web-api-v8/GUID-EA5DCD55-A957-4B47-9556-22A7920BD82F)*

# What are Websockets? 
* Websockets were created in 2011, and have rapidly gained popularity and utilization since.
* They allow 2-way communication between browser and server via a single TCP connection. 
* Compared to the normal HTTP ‘handshake’ protocol where the client has to request data from the server, this 2-way communication allows both the server-side and the client-side to initiate communication.

# What kind of features can we create using WebSockets?
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ch6gypl4iiwmhd6awspi.png)
Image source: *[Pusher Channels](https://pusher.com/channels)*

There are an incredible number of real-time features that we use and interact with in our everyday life.
These include
* Instant messaging
* Notifications
* Livestreaming
* Live Tracking (i.e taxi and delivery services)
* IoT devices (i.e smart devices)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4h9qb6x89n33smt66905.png)
Image source: *[Ably.io](https://www.ably.io/concepts/websockets)*

#### Why not just use AJAX calls?
Simply put, AJAX requests and responses do not create a persistent connection between client and server. This can create a significant strain on the server as user traffic increases. 
They are less reliable for message ordering and receiving. In some circumstances, the user may accidentally send messages twice or may not receive any message at all. 

#### Are there any drawbacks to using Websockets?
Some WebSocket libraries may not be supported by older browsers 
However, this is becoming less of an issue in 2020; 97% of browsers support Websockets.

# What WebSocket Libraries are Available and Which to Use?
There is a plethora of libraries and services available for adding real-time features to an app. Some of the more popular ones are:

##### Socket.IO
* Beginner-friendly- simplifies creation and management
* Fallback mechanisms include reverting to HTTP polling if the WebSocket is not able to connect, and auto-reconnection 
* Disconnection detection
* Ability to connect even if there are proxies, load balancers, firewalls, and antivirus software present
* Also available to use in Java, C++, Swift, Dart, Python, and .Net

##### Firebase
* A good choice for both mobile apps and web apps
* Accounts for scaling as your app grows
* Provides analytics and performance monitoring data
* Has many extensions and functions available for add-on. Some of which include machine learning, cloud storage, and Google Ads
* Also available in C++ and Unity

##### WS
* A  popular user/server library for NodeJS
* It is fast, simple, and well tested
* Supported by almost all browsers
* Has additional modules available

#### Is it worth paying for a Websocket API? 
A few APIs require a subscription for apps using their services that are over a certain traffic load. Scalability vs performance is a major hurdle to consider when building a web application. For projects with a large user base, it may be worth it to pay the subscription fee to easily eliminate this issue and channel the team’s energy into a more worthy cause, such as improving the application’s features.
 
##### Pusher
* Easy to implement
* Is free under a certain traffic threshold
* Boasts 99.997% Uptime
* Provides support to over 40 libraries and SDKs and prioritizes WebSocket support
* Allows your app to easily host millions of messages a day and thousands of concurrent users

##### PubNub
* Easy to implement
* Boasts 99.999% Uptime
* Provides support to over 70 libraries and SDKs
*Prioritizes HTTP transport protocol but has access to libraries that can implement a WebSockets protocol. Although, WebSockets are the more efficient method, this will ensure there are no connectivity issues regarding proxies, firewalls and anti-virus software.

# The Future of Websockets
Are Websockets being replaced by something bigger and better? As of 2021, not really. But there are a couple of newer real-time options that developers should keep an eye on. 

##### WebRTC
* A Javascript library tailored toward video and audio communication
* Does not require the user to download and install any plugins or apps

##### WebTransport
* An API that is aiming to be a faster and more reliable version of Websockets
* It is still relatively new and unstable but is continuing to be developed






# Resources

> 1. [Is this the end of WebSockets? - The New WebTransport Protocol](https://youtu.be/jvdg-jOYK5E)
2. [A Beginner's Guide to WebSockets - YouTube](https://www.youtube.com/watch?v=8ARodQ4Wlf4)
3. [A Starter Guide to Building Real-time Applications with Node.js](https://livecodestream.dev/post/a-starter-guide-to-building-real-time-applications-with-nodejs/)
4. [Why use AJAX when WebSockets is available?-Stack Overflow](https://stackoverflow.com/questions/10377384/why-use-ajax-when-websockets-is-available#:~:text=A%20better%20use%20for%20AJAX%20is%20REST%20API%20calls.&text=REST%20calls%20and%20uploads%20using,fragmentation%20%2F%20multiplexing%20logic%20is%20coded.)
5. [Building Real-Time Web Apps: Options in 2020 | by Artur Iatsko | Medium](https://medium.com/@arturyatsko/building-real-time-web-apps-options-in-2020-640c2c565383)
6. [Beyond REST: Using WebSockets for two-way communication in your React app - LogRocket Blog](https://blog.logrocket.com/beyond-rest-using-websockets-for-two-way-communication-in-your-react-app-884eff6655f5/)
7. [A WebSocket Implementation for Node.JS](https://github.com/theturtle32/WebSocket-Node)
8. [Fundamental performance and scalability concepts - Web application performance and scalability ](https://www.webforefront.com/performance/fundamentalconcepts.html)
9. [Using Firebase To Provide Real-time Notifications | by Amit Ashwini](https://medium.com/swlh/using-firebase-to-provide-real-time-notifications-2f86b2f7d499)
10. [Socket.IO](https://socket.io/)
11. [Firebase ](https://firebase.google.com/)
12. [WebRTC](https://webrtc.org/)
13. [WebRTC - Wikipedia](https://en.wikipedia.org/wiki/WebRTC)
14. [WebTransport](https://w3c.github.io/webtransport/#introduction)
15. [WebSockets vs Long Polling | Ably Blog: Data in Motion](https://www.ably.io/blog/websockets-vs-long-polling)
16. [How Do Websockets Work? - Kevin Sookocheff](https://sookocheff.com/post/networking/how-do-websockets-work/)
17. [10 Best WebSocket Libraries That You Should Know - Geeky Humans](https://geekyhumans.com/websocket-libraries/)