{"id":60317,"date":"2024-02-27T11:18:54","date_gmt":"2024-02-27T05:48:54","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=60317"},"modified":"2024-02-27T11:18:54","modified_gmt":"2024-02-27T05:48:54","slug":"understanding-websockets-with-socket-io","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/understanding-websockets-with-socket-io\/","title":{"rendered":"Understanding WebSockets with Socket.io"},"content":{"rendered":"<h2><strong>Introduction to<\/strong> <strong>WebSockets\u00a0<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">WebSockets is a communication protocol that enables real-time<em><strong> bi-directional<\/strong> <\/em>connections between server and client. It provides the ability to the server to provide updates to the client without the client explicitly requesting the server. <\/span><span style=\"font-weight: 400;\">WebSockets work internally by <\/span><span style=\"font-weight: 400;\">upgrading<\/span><span style=\"font-weight: 400;\"> the HTTP connection to a WebSocket connection. This Upgrade header signifies the WebSocket handshake.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60463\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-26-202930.png\" alt=\"\" width=\"473\" height=\"366\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-26-202930.png 783w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-26-202930-300x233.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-26-202930-768x595.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-26-202930-624x484.png 624w\" sizes=\"(max-width: 473px) 100vw, 473px\" \/><\/p>\n<h2><strong>Features of WebSockets &#8211;<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em><strong>\u25cf Low Latency<\/strong><\/em> &#8211; In WebSockets, the connection is alive unless either the client or server terminates it, unlike HTTP\/HTTPS protocol that follows a basic client-server model where the server sends a response only after receiving a request from the client and the connection gets terminated by itself after receiving the response.<br \/>\n<\/span><\/span><\/p>\n<p><strong><em>\u25cf Efficiency <\/em><\/strong>&#8211; In HTTP\/HTTPS protocol, the data is fetched via the <strong><em>Polling<\/em><\/strong> technique i.e., the client consistently sends requests to the server until it updates, which is not an optimal way to communicate in real-time as it creates much more load to the server. Web Sockets use a <i>streaming<\/i> technique where the client listens continuously with no external data request from the server. This reduces the load on the server as compared to the long polling technique.<\/p>\n<p><em><strong>\u25cf Real-Time updates<\/strong> <\/em>&#8211; WebSockets are well-suited for real-time applications like applications and trading platforms as they can support continual data transmission. They are also used in collaborative editing tools like Google Docs, where changes made by different users need to be synchronized. Thus, it is an optimal protocol for such real-time updates use cases where bi-directional communication is crucial.<\/p>\n<h2><strong>What is Socket.io?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Socket.io is an event driven library that helps in creating bi-directional communication in real time web applications by using websockets. Since this library is built on top of web sockets it <\/span><span style=\"font-weight: 400;\">provides a low-latency communication channel.<\/span><\/p>\n<h3><strong>Building a Chat App using Socket.io &#8211;\u00a0<\/strong><\/h3>\n<p><em><em><em>Building an Express server with Socket.io:<\/em><\/em><\/em><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60313 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-1024x717.png\" alt=\"\" width=\"625\" height=\"438\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-1024x717.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-300x210.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-768x538.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-624x437.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog.png 1122w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>Handling Socket.io connections and message-sending functionality on the server side:<br \/>\n<\/em><br \/>\nHere, \u2018io\u2019 is an object that represents the Socket.IO server and &#8216;io.on&#8217; is an event listener that is used to listen and handle different events that occur during the lifecycle of a web socket connection. The provided callback function to the event listeners is executed whenever an event is triggered.<\/span><\/span><\/span><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60316 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-3.png\" alt=\"\" width=\"752\" height=\"822\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-3.png 752w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-3-274x300.png 274w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-3-624x682.png 624w\" sizes=\"(max-width: 752px) 100vw, 752px\" \/><\/p>\n<p><em><em><em><em>Integrating Socket.io on the frontend:<\/em><\/em><\/em><\/em><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60315 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-2.png\" alt=\"\" width=\"621\" height=\"801\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-2.png 621w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/blog-2-233x300.png 233w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/p>\n<p><span style=\"font-weight: 400;\"><em>Running the Chat Application:<br \/>\n<\/em><\/span><span style=\"font-weight: 400;\"><br \/>\nRun <\/span><span style=\"font-weight: 400;\">both the server and the React app, open multiple browser tabs, and navigate to &#8216;<\/span><span style=\"font-weight: 400;\">http:\/\/localhost:3000&#8242;<\/span><span style=\"font-weight: 400;\"> to test the real-time chat functionality.<\/span><span style=\"font-weight: 400;\"> Messages sent from one tab will be broadcasted to all the other tabs as well; this signifies the real-time communication functionality achieved using web sockets.<br \/>\n<\/span><\/p>\n<h2><strong>Conclusion\u00a0 <\/strong><\/h2>\n<p>Thus, WebSockets are an important paradigm in building interactive, dynamic, and real-time applications on the web. The seamless, low-latency communication facilitated by WebSockets enhances the user experience significantly by minimizing the need for frequent HTTP requests and enabling instant data exchange. Though they might not completely replace the HTTP connection, they do offer a number of advantages over the conventional communication techniques in some use cases.<\/p>\n<p>Check out our other blog posts for more insights. If you still have any questions, comment and join the discussion.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction to WebSockets\u00a0 WebSockets is a communication protocol that enables real-time bi-directional connections between server and client. It provides the ability to the server to provide updates to the client without the client explicitly requesting the server. WebSockets work internally by upgrading the HTTP connection to a WebSocket connection. This Upgrade header signifies the WebSocket [&hellip;]<\/p>\n","protected":false},"author":1619,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":262},"categories":[3429,3038],"tags":[2046,4258,5676],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60317"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1619"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=60317"}],"version-history":[{"count":11,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60317\/revisions"}],"predecessor-version":[{"id":60470,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60317\/revisions\/60470"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=60317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=60317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=60317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}