<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Singhania Aditya on Medium]]></title>
        <description><![CDATA[Stories by Singhania Aditya on Medium]]></description>
        <link>https://medium.com/@singhania94?source=rss-a7e66b4e153b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*im-vtsauY_cx11nQ5kfsVg.jpeg</url>
            <title>Stories by Singhania Aditya on Medium</title>
            <link>https://medium.com/@singhania94?source=rss-a7e66b4e153b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 23:39:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@singhania94/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Browser Push Notification Service — The Challenges With the WebSocket Server]]></title>
            <link>https://medium.com/swlh/building-a-browser-push-notification-service-the-challenges-with-the-websocket-server-8cf9b1827e24?source=rss-a7e66b4e153b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cf9b1827e24</guid>
            <category><![CDATA[push-notification]]></category>
            <category><![CDATA[scalability]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[realtime]]></category>
            <dc:creator><![CDATA[Singhania Aditya]]></dc:creator>
            <pubDate>Tue, 08 Sep 2020 18:33:48 GMT</pubDate>
            <atom:updated>2020-09-12T21:55:38.027Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*e7BoXPb4Dz0SKQQYxU9UPA.png" /><figcaption>the question that only only people who build horizontally scalable software, would ask :P</figcaption></figure><h4>Building a Browser Push Notification Service — The Challenges With the WebSocket Server</h4><p><em>This is the final article in the trilogy series ‘Building a Browser Push Notification Service’, you can find the previous article that talks about the implementation of a websocket server </em><a href="https://medium.com/@singhania94/building-a-browser-push-notification-service-the-low-level-design-using-the-gorilla-websocket-aa8372f3e113?source=friends_link&amp;sk=9e847f6ebe3649211bcee745437d06fd"><em>here</em></a><em>.</em></p><p><strong>What are websocket servers, you ask?</strong> You can read all about it <a href="https://medium.com/swlh/building-a-browser-push-notification-service-http-long-polling-and-the-web-socket-protocol-5e83cd1420c1?source=friends_link&amp;sk=f8b0fdd9acd6fe7eab29fd2d611c2db3">here</a>.</p><p><strong>What’s so different here?</strong> WebSocket Servers are typically different than our HTTP servers. You remember the coding wizards always telling you to keep your servers <strong>stateless</strong>? Well, that’s what simplifies certain aspects of the HTTP server. Now that we have introduced a state in the server, here are the challenges.<br>1. <strong>Horizontal Scalability</strong> — The websocket server stores the connections. So now, say for example, our server ‘s1’ and server ‘s3’ have some active connections with the target ‘t’. Now, if we receive a notification ‘n’ that is to be sent to target ‘t’, we’ll have to somehow figure out and forward this notification to the servers ‘s1’ and ‘s3’, since these are the ones that have active connections with the target ‘t’. If it were stateless servers, a request could be simply routed to any of the servers, but since the servers have states, we’ll have to forward the requests on the particular servers. <strong>See the challenge here?</strong><br>2. <strong>New Connection</strong> <strong>Request Routing</strong> — For websocket servers, ideally we would want to equally distribute the active websocket connections handled by any server. This poses a challenge as a simple ‘Round-Robin’ algorithm would not help here. Say for example, we have 3 WS servers at this instant each handling about a few 100k connections. At this instant, a new WS server comes up. Now, to equally distribute the load, we would want to route all the incoming connection requests to the newly brought up server, until the active connection on this server matches the count on the other servers. <strong>See the challenge here?</strong><br>Let’s look at how we can solve these.</p><p><strong><em>Case in Point : </em></strong><em>So this is what we’re going to do, we’ll pick the same example as in the previous blogs. We’ll have a </em><strong><em>notification</em></strong><em> message ‘</em><strong><em>Hello, Alexander Hamilton!</em></strong><em>’ that we have to send to our </em><strong><em>target client</em></strong><em> named </em><strong><em>Alexander</em></strong><em>(’s browser client). We’ll assume this person has a laptop, a tablet and a desktop. He has our website ‘</em><strong><em>thefoundingfathers.com</em></strong><em>’ opened on a browser tab in all of these three devices. Going to this website has created websocket connections with our servers. We’ll assume we have three websocket servers. The target client ‘</em><strong><em>Alexander</em></strong><em>’ has three connections, connection 1 with WS (websocket) Server 1, and connection 2 and connection 3 with WS server 2, and no connections with WS server 3. So, as soon as our websocket service receives any notification for our target ‘</em><strong><em>Alexander</em></strong><em>’, we are to send the </em><strong><em>notification</em></strong><em> forward to all these </em><strong><em>three browser tabs (three connections)</em></strong><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DXEKk3aaaCdYO3hkX8pNlA.jpeg" /></figure><p><strong>The Setup — </strong>This<strong> </strong>setup here basically would have two kinds of incoming requests. One, the <strong>websocket connection requests</strong> (the right side)., and two, the <strong>push notification requests</strong> (on the other side). Both of these will be tagged with a target id.<br><strong><em>Use Case : </em></strong><em>So, for our case, the websocket side load balancer received three connection requests all tagged with target id ‘</em><strong><em>Alexander</em></strong><em>’. It forwarded the 1st connection request to WS server 1. It forwarded the 2nd and the 3rd request to WS server 2. <br>The notification side load balancer received one request for the notification ‘</em><strong><em>Hello, Alexander Hamilton!’, </em></strong><em>which was forwarded to the application service</em><strong><em>.</em></strong></p><p>In between the two load balancers, we would have three components, each horizontally scalable.</p><ol><li><strong>The cached storage system — </strong>This system stores the mapping between the target ids and the servers, i.e. the servers that hold an active connection with the target.<br><strong><em>Use Case : </em></strong><em>The cached storage system has the mapping, which was populated by the websocket servers when they received the connection requests. <br>‘</em><strong><em>Alexander</em></strong><em>’ &lt;-&gt; </em><strong><em>WS Server 1, WS Server 2</em></strong><em>.</em></li><li><strong>The application service — </strong>This service is used to query the cache system. All notification requests reach this service, to figure out the servers that hold a connection / multiple connections with the intended target. This service then forwards the requests to all these websocket servers. <br><strong><em>Use Case : </em></strong><em>The service, when it received the notification ‘</em><strong><em>Hello, Alexander Hamilton!</em></strong><em>’ for the target ‘</em><strong><em>Alexander</em></strong><em>’, it queried the cache system for the target and received </em><strong><em>WS Server 1, WS Server 2 </em></strong><em>in the response.</em></li><li><strong>The websocket servers — </strong>This is the server cluster that actually maintains all the active connections and pushes all the notification requests to the targets. Additionally, any time this server successfully registers or deregisters a websocket connection request, it accordingly updates the cache system to add / remove itself against the specific target.<br><strong><em>Use Case : </em></strong><em>These servers, specifically the </em><strong><em>WS server 1</em></strong><em> and the </em><strong><em>WS server 2</em></strong><em>, when they received the connection request from the target ‘</em><strong><em>Alexander</em></strong><em>’, they updated the cache map for the target. Later, the server 1 and 2, when they received the notification ‘</em><strong><em>Hello, Alexander Hamilton!</em></strong><em>’, they pushed it over the active connections against the target ‘</em><strong><em>Alexander</em></strong><em>’.</em></li></ol><p><strong>The Connection Request Routing concerns</strong> — For the new connection requests, the connection request Load Balancer can simply forward the request to the WS server that has the least active connection count. This way we can make the best effort that the load is equally distributed among the WS servers.</p><p>With these implementation in place, we can horizontally scale to handle any number of websocket connections and notifications.</p><p>In conclusion, the websocket connections for long now, have been present in the technology basket. Still, for reasons, there are a lot of problems thatpeople face and end up designing sub optimal systems. These series of articles is an attempt to help people avoid some of these.<br>Got more suggestions or ideas even better? Reach out to me and let’s have a conversation! Happy developing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cf9b1827e24" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/building-a-browser-push-notification-service-the-challenges-with-the-websocket-server-8cf9b1827e24">Building a Browser Push Notification Service — The Challenges With the WebSocket Server</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Browser Push Notification Service — The Low-Level Design Using the Gorilla/Websocket…]]></title>
            <link>https://medium.com/swlh/building-a-browser-push-notification-service-the-low-level-design-using-the-gorilla-websocket-aa8372f3e113?source=rss-a7e66b4e153b------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa8372f3e113</guid>
            <category><![CDATA[realtime]]></category>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[push-notification]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Singhania Aditya]]></dc:creator>
            <pubDate>Sun, 30 Aug 2020 17:38:11 GMT</pubDate>
            <atom:updated>2020-09-12T21:55:21.138Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lXkCRMpRSMcQFi2gLKkGCQ.png" /><figcaption>gopher leaving everyone awestruck with it’s magic of go-routines and go-channels</figcaption></figure><h3>Building a Browser Push Notification Service — The Low-Level Design Using the Gorilla/Websocket Library in Golang</h3><p><em>This is a follow up article in the series ‘Building a Browser Push Notification Service’, you can find the previous article that talks about the basics of websocket connection and push notifications </em><a href="https://medium.com/@singhania94/building-a-browser-push-notification-service-http-long-polling-and-the-web-socket-protocol-5e83cd1420c1?source=friends_link&amp;sk=f8b0fdd9acd6fe7eab29fd2d611c2db3"><em>here</em></a><em>.</em></p><p><strong><em>Case in Point : </em></strong><em>So this is what we’re going to do, we have a </em><strong><em>notification</em></strong><em> message ‘</em><strong><em>Hello, Alexander Hamilton!</em></strong><em>’ that we have to send to our </em><strong><em>target client</em></strong><em> named </em><strong><em>Alexander</em></strong><em>(’s browser client). For completeness sake, let’s assume this person has a laptop, a tablet and a desktop. He has our website ‘</em><strong><em>thefoundingfathers.com</em></strong><em>’ opened on a browser tab in all of these three devices. Going to this website would also create a websocket connection with our server. So, as soon as our websocket service receives any notification for our target ‘</em><strong><em>Alexander</em></strong><em>’, we are to send the </em><strong><em>notification</em></strong><em> forward to all these </em><strong><em>three browser tabs (three connections)</em></strong><em>.</em></p><p>In this article, we’ll try to see what the low level design of the websocket server could look like, with the <a href="https://github.com/gorilla/websocket">gorilla/websocket</a> library (lets not fry our brains with the scenarios of having multiple websocket servers, yet, and first solve how it would work with a single websocket server). We’ll rely immensely on two essential resources provided by the go language — the <strong>go-routines</strong> and the <strong>go-channels</strong>.</p><p><strong>What are go-routines and go-channels, you ask?</strong> Well, the golang language is known to have greatly eased the work of having a multithreaded architecture, both for the developer and also from the resource utilization angle (memory and CPU usage). It has provided two things to help this cause, the <a href="https://golangbot.com/goroutines/"><strong>go-routines</strong></a>, which are the methods that can run concurrently with other methods, basically given a fancy name and a makeover. These can be thought of very light weight threads (~2kb). And the <strong>go-channels, which </strong>are the communication wires between these go-routines. <br><strong>So essentially, if you want to run something concurrently?</strong> Simply create a new <strong>go-routine</strong> (which is super cheap and easy). <br><strong>Now, you want to communicate between these go-routines? </strong>Avoid sharing the memory or the variables between these routines, you’ll probably end up frustrated, trying to resolve issues that come with concurrency (race conditions etc.). Instead, pass this variable over something like the communication wire, to the intended go-routines, and this communication wire is <strong>go-channels</strong> (in-built in go), which passes all the tests of concurrency. This is inline with the language’s philosophy that says <em>“Don’t communicate by sharing the memory, share the memory by communicating”. </em>The channels are like having a queue to communicate between two services, just that this one’s going to reside in your memory, and will help you to communicate between your threads.</p><p><strong>Okay, got the terms, now how does the low level design look like? </strong>Well it would look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9VXDCvMeOxiCGSrMDg_UeA.jpeg" /></figure><p><strong>The go-routines and the channels</strong> — Here in this architecture, the different boxes denote the different types of worker pools that will run in our server. <br>The worker pools are basically like the different departments in a company. Like how the department employs an employee to get the job done, each worker pool may employ 1 or more workers (go-routines) to get the job done. <br>These company departments need to constantly communicate with each other to achieve the larger company objective. Likewise, the arrows between these workers denote the communication wires layed out for the workers to talk to each other (or the go-channels), which collectively achieve the objective of delivering the notification to our targets. The pool of workers will try to make some request to some other pool to take care of the next part of the processing, and these requests will be sent over the channels. <br>Let us have a look at the different types of worker pools and their jobs.</p><ol><li><strong>Source (connection upgrade request) worker pool</strong> <strong>(HTTP server)</strong>— This worker pool processes the incoming connection upgrade requests. It will look at the targetID, the request fields and the headers, and decide if the request is worthy of a websocket upgrade (based on the origin of the request, the authentication credentials, etc.). It will accordingly approve or reject the request. <br>Rejecting the request would simply mean passing back a non OK status as an HTTP response.<br>While approving the request would mean three things<br>a.<strong> Sending an HTTP OK status</strong> in the response.<br>b.<strong> Forwarding the connection object to the ‘Hub pool’ of workers</strong>, this is what we will call a <em>registration request</em> to the hub. More on this in Hub pool section. <br>c. <strong>Starting two go-routines</strong> (a read pump go-routine and a write-pump go-routine), for handling the reads and writes for this connection. More on this in the read pump / write pump worker pool section.<br><strong>The ‘source (connection upgrade request) worker pool’ can have any number of go-routines running based on the incoming connection-upgrade-requests traffic.<br><em>For our case in point, the person Alexander sent a connection upgrade request tagged with the target ‘Alexander’ as soon as he opened our website on any of the deivces. This worker then forwarded these connection registration requests to the hub pool. It would also start a read pump and a write pump, against each one of these connection requests.</em></strong></li><li><strong>Source (notification request) worker pool (HTTP / GRPC or Queue worker)</strong> — This worker pool sources the notification requests. These requests will be tagged with the targetID (the intended receiver). This pool will simply forward all the valid notification requests to the ‘Hub pool’ for further processing.<br><strong>The ‘source (notification request) pool’ can have any number of go-routines based on the incoming notifications-traffic.<br><em>For our case in point, this worker received a notification ‘Hello, Alexander Hamilton!’ tagged against the target ‘Alexander’. This worker then forwaded the notification request to the hub pool.</em></strong></li><li><strong>The Hub pool</strong> — This worker pool, as you might have seen is the most critical component in the architecture. This stores all the current active connections (active connections meaning, a connection that had been upgraded to a websocket and has not been closed yet) in a <em>map[targetID]connections</em>. All the notifications are to be sent over this connection object. The hub takes care of three things.<br>a. For a connection <strong>registration</strong> request, store the new connection in the map against the given targetID. <br>b. For a connection <strong>deregistration</strong> request, remove the connection from the map. (<em>Who sends this, you ask</em>? Well, it may originate mostly from the read pump or the write pump. More on this in the relevant sections.) Additionally, this will also send a close-go-routine-request to the read and the write pump go-routines that run for this connection.<br>c. For a <strong>notification</strong> request, it will search the map and get the active connections stored against that targetID, and forward the request to all the write pumps running for those connections. If it does not have any active connection against the targetID, it can simply discard the notification.<br>So, overall, if you see, this pool takes care of any and all read / write operations on the map.<strong><br>Unfortunately, the Hub pool will only have a single worker go-routine., since the map in golang does not support concurrent operations inherently.<br><em>For the case in point, the connection registration requests would end up populating the connections in the map, as [Alexander]three-connections. Additionally, when the notification request comes to this hub, it would figure that there are three connections against the target ‘Alexander’ and would would forward the request to all of these three connections’ write pumps.</em></strong></li><li><strong>The Read pump — </strong>This go-routine will be created one per connection immediately after the connection upgrade request is approved. This routine will continuouly keep polling over the connection object, to check if there’s data sent over the connection by the target client (if you remember the earlier <a href="https://medium.com/@singhania94/building-a-browser-push-notification-service-http-long-polling-and-the-web-socket-protocol-5e83cd1420c1?source=friends_link&amp;sk=f8b0fdd9acd6fe7eab29fd2d611c2db3">article</a>, we’d seen that websockets enables a duplex communication). It will then forward the received notification to the appropriate processor. Additionally, as soon as this pump gets some error while reading over the connection, this will send a deregistration request for this connection to the hub routine.<br><strong>The read pump go-routine, as we’ve discussed, runs one per active connection.</strong></li><li><strong>The Write pump </strong>— The write pump will also be created one per connection, which handles the work of the last mile delivery of the notification through the connection. The notifications forwarded by the hub are, in this routine, written to the connection buffer by this pump. Here also, as we’d seen in the read-pump, as soon as it gets some error while pushing over the connection, it will send a deregistration request for this connection to the hub routine.<br><strong>The write pump go-routine, runs one per active connection.<br><em>For our case in point, the write pump against each of the three connections would receive the notification ‘Hello, Alexander Hamilton!’ and they would simply write this over the connection that they’re running for.</em></strong></li></ol><p>Now, for all of these requests that the workers have to make to each other, channels are extensively used. As in the diagram, the channels used are-<br>1. Hub Connection Registration Request Channel<br>2. Hub Connection De-Registration Request Channel<br>3. Hub Write Notification Channel<br>4. Write Pump Write Notification Channel (1 per connection)<br>5. (optional) Read Pump Received Notification Channel</p><p>Now that we understand the internal design of our websocket server, another interesting challenge worth looking at is the horizontal scaling of websocket server. On a high level, if you’ve worked with API servers you’ll know it’s easy to scale them since they’re all stateless. But here, all of our servers hold some state (the active connections). And if at all we want to have multiple servers, we’ll have to handle-<br>1. Forwarding the notifications to only and all of those servers which have an active connection with that target that the notification is intended for.<br>2. Route the new websocket connection request to the websocket server, based on some advanced logic rather than just round robin, so that the number of active connections are evenly distributed among the servers.<br>More about this in the following article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kWSsAF6EfXHbCBQ8xWIDQQ.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa8372f3e113" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/building-a-browser-push-notification-service-the-low-level-design-using-the-gorilla-websocket-aa8372f3e113">Building a Browser Push Notification Service — The Low-Level Design Using the Gorilla/Websocket…</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Browser Push Notification Service — Long Polling and the Web Socket protocol]]></title>
            <link>https://medium.com/swlh/building-a-browser-push-notification-service-http-long-polling-and-the-web-socket-protocol-5e83cd1420c1?source=rss-a7e66b4e153b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e83cd1420c1</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[realtime]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[push-notification]]></category>
            <category><![CDATA[websocket]]></category>
            <dc:creator><![CDATA[Singhania Aditya]]></dc:creator>
            <pubDate>Sat, 29 Aug 2020 14:31:07 GMT</pubDate>
            <atom:updated>2020-09-07T23:10:25.917Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*jsuMDBzdjU2ypJ4yGb9LSA.jpeg" /><figcaption>a gopher flying faster than the plane to deliver a push notification</figcaption></figure><h4><a href="https://medium.com/@singhania94/building-a-browser-push-notification-service-http-long-polling-and-the-web-socket-protocol-5e83cd1420c1?source=your_stories_page---------------------------">Building a Browser Push Notification Service — Long Polling and the Web Socket protocol</a></h4><p>What’s the ideal pattern /design / tech stack for a push notification service (also, what’s a push notification?) (using websocket, also, what’s a websocket?) that would allow efficient usage of resources, be lightning fast, and also scale horizontally? In these series of blogs, lets try to break this down into smaller chunks and answer them individually.</p><p>Before we dive deep into the details, lets gets some terminologies clear.</p><p><strong>What are websockets / websocket protocol?</strong> <br>`<strong><em>WebSocket</em></strong><em> is a computer </em><a href="https://en.wikipedia.org/wiki/Communications_protocol"><em>communications protocol</em></a><em>, providing </em><a href="https://en.wikipedia.org/wiki/Full-duplex"><em>full-duplex</em></a><em> communication channels over a single </em><a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol"><em>TCP</em></a><em> connection`</em>. Lets try to take the sophistication out from the previous sentence, and understand the basics. WebSocket is simply another communication protocol like HTTP, or TCP. HTTP works in a simple client-request - server-response manner, for example, it could be like the militants talking over a radio, where, the personnel sitting at the base asks for information, only after which the person on the field responds with the answers, and the other way around is not allowed. (You never question your superiors in the army :P)</p><p>WebSocket protocol allows both the client (or as we call in this article, the target) and the server to send any piece of information to each other at any point in time, provided the connection is established. Similar to you talking to your friend over a call, where either of you can talk and /or listen, once the call is placed, anytime.</p><p><strong>What is a push notification? <br></strong>As far as this blog is concerned, a push notification is simply some piece of information / data you want to convey to your target client, without the client explicitly asking for it. Like for example, when you open your mail, anytime you get a new mail, you would like to see a popup on the top of the tab that says ‘You have a new mail’. Now here, the browser tab (our client / target) did not ask the backend if you have a new mail. Rather, the backend realized that someone sent you a mail, and now wants to send you this information / notification. This type is what we’re calling push notification here.</p><p><strong>The Use Case</strong>: Now lets talk of a use case. Say, you have a desktop dashboard. Now, you want to enable <strong>push notifications</strong> from the backend servers to this desktop UI (which is opened on a browser tab). This is slightly different compared to the usual requirements out of APIs, where the <strong>UI</strong> (<strong>target</strong>) makes a specific request to our servers and the data is sent back as a response of it (classic use case of HTTP protocol). For push notifications, however, the client has not made a request, but instead, the backend is the initiator and it said, hey, I want to send this <strong>information</strong> (<strong>notification</strong>) that is important for the target (UI) to see right away, like we saw for the new email notification. Lets see how we can enable this.</p><p><strong>Push Notification with long polling</strong>: Now that we get the peculiarity of this requirement, let us brainstorm about how we can get this done. One simple way could be <a href="https://www.ably.io/concepts/long-polling"><em>long</em> <em>polling</em></a><em>. </em>Meaning, the target simply sends an HTTP request to the backend. The backend, instead of responding to the request immediately, just keeps it in pending state. Now, only when there is a push notification to be sent to a target, the backend server figures the pending HTTP requests sent by that target and now, sends the push notification as a response. This is actually how the push notifications were handled traditionally. For multiple reasons, in the <em>smart-ass-land</em>, this method is now looked down upon, mostly because of how intensive it turned out to be on the server side.</p><p><strong>Push Notification with WebSockets: </strong>There’s a better solution possible. Say, how about we just register and persist with the server, the connection aganist the targetID instead? Meaning, the target UI, as soon as is fired up, a connection is made and persisted with the push-notification service. The target, keeps listening over this connection, if there’s any new notification and then processes it accordingly. The target and the push-notification service can both keep polling at effective intervals to make sure the connection is functional for robustness. If the connection is detected dead by the target, it can simply make a new connection request. And if the server detects a dead connection, it simply cleans the connection related objects from it’s memory. This exact philosophy is harvested in the websocket protocol.</p><p><strong>How is the WebSocket communication channel created, an example?</strong> Imagine this like you are walking up to a bank, and requesting to open a Savings Account, and at the same time, requesting an upgrade for thisnewly created account that allows you to have a credit card as well. The bank will then authenticate your documents, check for your CIBIL score and approve or deny your request accordingly. If approved, an account will be created and a credit card alloted, so you can make transactions over it. Also, if you fail to make transactions for a consecutive period of three months say, your account may be marked dormant and later closed.</p><p><strong>How is the WebSocket communication channel actually created? </strong>Similarly, for a websocket connection to be established, the target makes an HTTP request to the server backend, with something called an ‘<strong>Upgrade</strong>’ header set to convey that the client wants to upgrade the communication protocol to WebSocket. The server then checks for the authentication credentials, and accordingly approves the upgrade / denies it (and closes the connections). If the upgrade is approved, the target gets a confirmation after which it can continuously listen over this connection for any new notifications. In addition, there is continuous polling to ensure that the connection has not gone ‘<em>dormant</em>’ or like we call it, it has not become a dead conenction.</p><p><strong>In conclusion</strong>, for multiple reasons, the websocket connections turn out be efficient in terms of resource consumption on the server side, greatly reducing the costs. The details about the comparison about the ways to enable push notification, can be in itself, another article. But for now, in the following articles, we’ll try to address the challenges faced when developing a websocket server (<a href="https://medium.com/@singhania94/building-a-browser-push-notification-service-the-low-level-design-using-the-gorilla-websocket-aa8372f3e113?source=friends_link&amp;sk=9e847f6ebe3649211bcee745437d06fd"><em>here</em></a>).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e83cd1420c1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/building-a-browser-push-notification-service-http-long-polling-and-the-web-socket-protocol-5e83cd1420c1">Building a Browser Push Notification Service — Long Polling and the Web Socket protocol</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>