<?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 Shayan on Medium]]></title>
        <description><![CDATA[Stories by Shayan on Medium]]></description>
        <link>https://medium.com/@sh4yan?source=rss-e50b596f6f13------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zVqhHeSvR72Sg2OSOmLJiw.jpeg</url>
            <title>Stories by Shayan on Medium</title>
            <link>https://medium.com/@sh4yan?source=rss-e50b596f6f13------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:00:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sh4yan/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[Get Push Notifications from JavaScript in Just One Minute!]]></title>
            <link>https://javascript.plainenglish.io/get-push-notifications-from-javascript-in-just-one-minute-1ae5c221f82b?source=rss-e50b596f6f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ae5c221f82b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Shayan]]></dc:creator>
            <pubDate>Fri, 28 Jan 2022 15:02:49 GMT</pubDate>
            <atom:updated>2022-01-31T12:15:19.719Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Vnhye_Cyn7anF4vmpAeatQ.png" /></figure><p>There have been many times when, as a developer, I wanted to be notified and track certain events that happened within my projects. For example, when a user joins a newsletter, creates an account, upgrades to a premium plan, or provides feedback.</p><p><a href="https://logsnag.com/">LogSnag</a> makes it very easy to set up these notifications and creates feeds of events so you can be aware of what happened and when it happened.</p><h4>Getting Started</h4><p>First, I will add a new project to my LogSnag account. Let’s call it <strong><em>my-saas</em></strong> for this example.</p><figure><img alt="create a new project called my-saas" src="https://cdn-images-1.medium.com/max/1024/1*qjhLEKQcRYxQz3yiKNbw-A.gif" /></figure><p>Next, we need an API token. Head to the settings, open the API tab, and use the <strong><em>+</em></strong> button to create a new token. You can then use the clipboard icon to copy the token.</p><figure><img alt="create a new API token." src="https://cdn-images-1.medium.com/max/1024/1*mUbGvf7U5oqLY6B-zYe-TA.gif" /></figure><p>We are almost done! Let’s move to our code!</p><h4>JavaScript Time!</h4><p>First, let’s install the <a href="https://www.npmjs.com/package/logsnag">LogSnag npm package</a></p><pre>npm install --save logsnag</pre><p>Then, we have to import the package and initialize our client with the API token that we just copied from the application.</p><pre>import { LogSnag } from &#39;logsnag&#39;;</pre><pre>const logsnag = new LogSnag(&#39;MY_API_TOKEN&#39;)</pre><p>Finally, we can use our client to publish any events from our application.</p><p>For this example, I will call my channel <strong><em>waitlist</em> </strong>as I would like to be notified and keep track of users who join my waitlist. Since this is the first time we publish to this channel, LogSnag will automatically create it for us.</p><p>I’m going to pass in the user email in the description and use the unicorn emoji as the icon. Most importantly, I will set notify to true since I would like to receive a push notification for this event.</p><pre>logsnag.publish({<br>    project: &quot;my-saas&quot;,<br>    channel: &quot;waitlist&quot;,<br>    event: &quot;User Joined Waitlist&quot;,<br>    description: &quot;email: john.doe@yahoo.com&quot;,<br>    icon: &quot;🦄&quot;,<br>    notify: true<br>})</pre><p>Once we run this code, a new channel is created under the <strong><em>my-saas</em></strong> project and we get push notifications for this event on all of the devices that have <a href="https://logsnag.com/">LogSnag</a> installed!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YYznDPq6RsCMp4x5Z0sEMw.gif" /></figure><p>LogSnag has been a side project for the past couple of months. It has originated from the pain points of using messaging platforms to publish and track user activity and events. LogSnag has been explicitly designed for this purpose and provides powerful features that make it much easier to track events and projects. Currently, LogSnag is in the beta stage, and you can get access by signing up for the <a href="https://logsnag.com/">waitlist on the website</a>.</p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Get exclusive access to writing opportunities and advice in our </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>community Discord</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ae5c221f82b" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/get-push-notifications-from-javascript-in-just-one-minute-1ae5c221f82b">Get Push Notifications from JavaScript in Just One Minute!</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use Python or JavaScript to Send Your Events and Logs to Telegram via their Chatbot API]]></title>
            <link>https://javascript.plainenglish.io/use-python-or-javascript-to-send-your-events-and-logs-to-telegram-via-their-chatbot-api-57f407f2c7e3?source=rss-e50b596f6f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/57f407f2c7e3</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[telegram]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Shayan]]></dc:creator>
            <pubDate>Tue, 09 Nov 2021 20:30:39 GMT</pubDate>
            <atom:updated>2021-11-11T06:20:18.934Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Publish your events and logs to Telegram" src="https://cdn-images-1.medium.com/max/1024/1*FzioGdLTCW0jOEDn28kMMQ.png" /></figure><p>In my opinion, Telegram has one of the best, if not the best, chatbot API. What they have created is extremely easy to use, yet it is unbelievably powerful. And I have used it for dozens of small projects over the years. One of my prominent use cases for their API is to send myself events and logs from my applications and projects.</p><p>Consider this example; you are developing a new project for which you may need to crawl hundreds or thousands of web pages or any other similar long-running tasks. You would deploy this script to a VM, or maybe your machine, and let it run for the next day or two. But, how do you usually check on the progress? Do you ssh to the VM, find the right tmux session, and read the logs? What if you want to leave home and don’t have access to the computer? Well, this is one of those situations that made me think… “hmm, what if I could use Telegram as a remote output?” I mean, the API is very, very easy to use, and all I need to do is to make an HTTP request to their endpoint, so why not?</p><p>This is just one of the dozens of examples where I found it convenient to push events to myself, and ever since, I have used it for many different projects to update myself on user actions, product sales, hell even stock prices. So in this blog post, I will be going over how to set up your first chatbot and how to send events to yourself via Python or JavaScript :)</p><h4><strong>Register a Telegram Chatbot.</strong></h4><p>Alright, the first step is to message <a href="https://t.me/botfather">@botfather</a> on Telegram and to create your first chatbot! The process is very straightforward, as you can see in the following screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1l9X4IioSxQuTSwkiuec0A.png" /></figure><p>Let’s copy the API token and add it to our environment variables. You can, of course, skip this process and add it directly to your code. Also, make sure to keep this token private! I will be revoking mine after writing this post.</p><pre>export BOT_TOKEN=2104030722:AAGdY_FeAFqvriecqv3lhissc-uG4t0arL4</pre><h4>Find your telegram Chat Id</h4><p>Ok, we have one more step to do before getting our hands dirty with some code. Thankfully this one is straightforward, we have to find our <strong>chat_id</strong> in Telegram, and there are multiple ways to find it. I will be using the chatbot API to find out my <strong>chat_id</strong>.</p><p>So, open the chatbot we have just created and send it a message; it could be anything. Then, make a get request to the following URL with your API token, using your browser or postman, and it should tell you your <strong>chat_id</strong>.</p><pre>// template<br><a href="https://api.telegram.org/bot">https://api.telegram.org/bot</a>&lt;BOT_TOKEN&gt;/getUpdates</pre><pre>// how mine looks like with my bot token<br><a href="https://api.telegram.org/bot2104030722:AAGdY_FeAFqvriecqv3lhissc-uG4t0arL4/getUpdates">https://api.telegram.org/bot2104030722:AAGdY_FeAFqvriecqv3lhissc-uG4t0arL4/getUpdates</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qPnpzDX_DGK3nmL-H-LOpQ.png" /></figure><p>As you see in the screenshot, you can see your <strong>chat_id</strong> via the following path <strong>result[].message.chat.id</strong>. Copy and again, add it to your environment variables or paste directly in the code.</p><pre>export CHAT_ID=&lt;MY_CHAT_ID&gt;</pre><p>Ok, that’s pretty much all we had to do with Telegram; let’s write some code. For this post, I will be providing an example with Python and JavaScript, so feel free to skip to whichever one you prefer.</p><h4>Publishing Events via Python</h4><p>First, let’s access our environment variables and assign them to variables</p><pre>import os</pre><pre>BOT_TOKEN = os.environ.get(&#39;BOT_TOKEN&#39;)<br>CHAT_ID = os.environ.get(&#39;CHAT_ID&#39;)</pre><p>I will be using the <a href="https://pypi.org/project/requests/">requests</a> package to handle my HTTP requests. So, let’s also install that via PyPi by running the following command.</p><pre>pip install requests</pre><p>Awesome! Now let’s define a method for sending our events to Telegram. We’re going to call it <strong>send_message</strong>, and it will need to accept a message string.</p><pre>def send_message(msg):</pre><p>Then, we will add Telegram’s send message endpoint and populate it via our bot token.</p><pre>url = f”https://api.telegram.org/bot{BOT_TOKEN}/sendMessage&quot;</pre><p>Next, let’s define the URL parameters that we need to send to Telegram. We need a chat_id, and text and by now, we should have both ready.</p><pre>params = { “chat_id”: CHAT_ID, “text”: msg }</pre><p>Finally, we will import requests and make a GET request and pass in our URL parameters.</p><pre>requests.get(url, params=params)</pre><p>Putting these all together, our method should look like the following.</p><pre>import requests</pre><pre>def send_message(msg):<br>   url = f&quot;https://api.telegram.org/bot{BOT_TOKEN}/sendMessage&quot;<br>   params = { &quot;chat_id&quot;: CHAT_ID, &quot;text&quot;: msg }<br>   requests.get(url, params=params)</pre><p>That’s pretty much it :) Now we can send whatever event that we have to ourselves through Telegram. So, remember the crawler job? Imagine we want to send periodic progress. We can do something like this:</p><pre>send_message(“🔥 Crawling progress: 56% done”)</pre><p>Once we run our code, if everything goes right, we should see something like this. How cool is that?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gVLkkUAGCGER6JuU3QGpnQ.png" /></figure><h4>Publishing Events via JavaScript</h4><p>Ok, let’s repeat the same process in JavaScript. First, let’s access our environment variables and set them to variables.</p><pre>const BOT_TOKEN = process.env.BOT_TOKEN<br>const CHAT_ID = process.env.CHAT_ID</pre><p>Next, I will be using the <a href="https://www.npmjs.com/package/axios">Axios</a> package to handle my HTTP requests. You can install via NPM by running the following command.</p><pre>npm i axios</pre><p>Now, let’s define our sendMessage method. It needs to take our string message as an argument. Then we will add the endpoint URL and populate it via our <strong>BOT_TOKEN</strong>. Next, let’s define our URL parameters required by Telegram; <strong>chat_id</strong> and <strong>text</strong>. Finally, we can make our get request via Axios and pass in our params.</p><pre>import axios from &#39;axios&#39;;</pre><pre>const BOT_TOKEN = process.env.BOT_TOKEN<br>const CHAT_ID = process.env.CHAT_ID</pre><pre>async function sendMessage(message) {<br>    const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`<br>    const params = { chat_id: CHAT_ID, text: message }<br>    await axios.get(url, { params: params })<br>}</pre><p>That’s pretty much it! Let’s give this one a shot as well.</p><pre>await sendMessage(&quot;💰 New user signed up&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CmoNY3Be9OkHHywCop23LA.png" /></figure><p>That’s pretty much it! By adding these couple of lines, we get another superpower that we can use for almost anything!</p><p>I have written a similar article for using Discord instead of Telegram. You can check it out via the following link if you’re interested :)</p><p><a href="https://sh4yan.medium.com/how-to-send-your-events-and-logs-to-discord-via-python-or-javascript-6be2859234c2">How to send your events and logs to discord via Python or Javascript.</a></p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57f407f2c7e3" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/use-python-or-javascript-to-send-your-events-and-logs-to-telegram-via-their-chatbot-api-57f407f2c7e3">Use Python or JavaScript to Send Your Events and Logs to Telegram via their Chatbot API</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Send your Events and Logs to Discord via Python or JavaScript]]></title>
            <link>https://javascript.plainenglish.io/how-to-send-your-events-and-logs-to-discord-via-python-or-javascript-6be2859234c2?source=rss-e50b596f6f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/6be2859234c2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Shayan]]></dc:creator>
            <pubDate>Mon, 08 Nov 2021 17:31:12 GMT</pubDate>
            <atom:updated>2021-11-10T07:07:48.192Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Sending events and logs to your devices using discord." src="https://cdn-images-1.medium.com/max/1024/1*AJabWJzlNE8kxhPVeciAgQ.png" /></figure><p>Throughout my software development career, there were many times when I wanted to get instant updates and alerts when something happened within my project.</p><p>Take this, for example; you are developing a SaaS, and there are multiple valuable events that you would like to be aware of as soon as they happen. For example, users joining your waitlist or newsletter, user sign-ups, product sales, and user conversions. Having to run a long training or crawling task on a remote machine was another situation where I wanted instant updates on the progress and if something had gone wrong. I mean, you can do periodic checks on the machine to see how things are going, but I’d rather entirely forget about it and have it send me updates instead. The last example is when I wanted to automate my garage door via raspberry pi and I wanted to know when the garage door was opening, closing, or had been left open for too long.</p><p>So why am I telling you these examples? I want you to start thinking about similar situations that you may also need a way to send yourself instant updates and to have a history of all these events in a single place.</p><p>Discord makes it relatively easy to solve this problem! We can create a Discord server specifically, create separate channels for our projects and use the webhook URL to push our events!</p><p>To get started, we need that Discord server. Log in to Discord, click on the <strong>Add Server</strong> button and proceed to create your own. Once that’s done, you should be able to see and open your Discord server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*52Vk44NECITo7iJOTeIkmA.png" /></figure><p>To make it easier to organize my projects and events, I usually create a new text channel for each project or, in some cases, one per type of event. To do so, click the <strong>+</strong> button next to text channels and create one. I’m going to call mine <strong>garage-door</strong> and make it a private channel.</p><p>Then, click on the settings icon for that channel, open the <strong>integration</strong> tab and create a new Webhook. Once added, you can see the <strong>Copy Webhook URL</strong>, and that’s precisely what we need to push our events! So copy that and paste it somewhere safe on your machine. Now, we are pretty much done with Discord, and we can move to writing some code! I have provided examples for Python and JavaScript; feel free to skip to whichever you find more relevant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2BJAIH9AQba1iVJE3kmjfA.png" /></figure><h3>Sending events via Python</h3><p>First, I would like to pass in my webhook URL as an environment variable, so I will set it to <strong>WEBHOOK_URL</strong> and use the standard library in python to access the value. You can, of course, skip this entire process and add your URL directly to the code.</p><pre>export WEBHOOK_URL=https://discord.com/api/...</pre><p>Once we have set the environment variable, we can access it by importing <strong>os</strong> and using the <strong>os.environ.get</strong> method.</p><pre>import os</pre><pre>WEBHOOK_URL = os.environ.get(&#39;WEBHOOK_URL&#39;)<br>print(WEBHOOK_URL)</pre><p>I will be using the <a href="https://pypi.org/project/requests/">requests</a> package to handle my HTTP requests. You can install via PyPi by running the following command.</p><pre>pip install requests</pre><p>Finally, all we need to do is to import <strong>requests</strong> and make a <strong>POST</strong> request to the webhook url and pass our event in the JSON body with the <strong>content</strong> key.</p><pre>import requests</pre><pre>requests.post(WEBHOOK_URL, { &quot;content&quot;: &quot;🦄 garage door is open&quot; })</pre><p>Once we run this code, we should get a new message in the <strong>garage-door</strong> channel telling us that our garage door has been opened.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fdmNPQ_7LazQX7mFB9iF0A.png" /></figure><h3>Sending events via JavaScript</h3><p>The process here is very similar to what we did in Python. First, define a new environment variable called <strong>WEBHOOK_URL</strong> for the webhook URL that we copied from discord. Again, you can skip this step and directly set the URL to a variable.</p><pre>export WEBHOOK_URL=https://discord.com/api/...</pre><p>Then, read this value <strong>process.env.WEBHOOK_URL</strong> and set it to a variable.</p><pre>const WEBHOOK_URL = process.env.WEBHOOK_URL<br>console.log(WEBHOOK_URL)</pre><p>I will be using the <a href="https://www.npmjs.com/package/axios">Axios</a> package to handle my HTTP requests. You can install via NPM by running the following command.</p><pre>npm i axios</pre><p>Finally, we can import <strong>Axios</strong> and make a <strong>POST</strong> request to the webhook url and pass in our event in the JSON body.</p><pre>import axios from &#39;axios&#39;;</pre><pre>await axios.post(WEBHOOK_URL, { content: &quot;💰 User sign up&quot; })</pre><p>Once we run this code, we should see another message in our Discord channel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*E6xbFC565HPeiJhWcK0LcA.png" /></figure><p>As you can see, getting this set up is very easy while being quite powerful! I use this setup in almost all of my projects to be aware of how they are doing!</p><p>I have written a similar article for using Telegram instead of Discord. You can check it out via the following link if you’re interested :)</p><p><a href="https://sh4yan.medium.com/use-python-or-javascript-to-send-your-events-and-logs-to-telegram-via-their-chatbot-api-57f407f2c7e3">Use Python or Javascript to send your events and logs to Telegram via their Chatbot API.</a></p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6be2859234c2" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/how-to-send-your-events-and-logs-to-discord-via-python-or-javascript-6be2859234c2">How to Send your Events and Logs to Discord via Python or JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I Created an Event-Driven Backend with RxJS, Server-Sent Events, Express, and Node.js]]></title>
            <link>https://javascript.plainenglish.io/how-i-created-an-event-driven-backend-with-rxjs-server-sent-events-and-expressjs-9f8be1ffc123?source=rss-e50b596f6f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f8be1ffc123</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Shayan]]></dc:creator>
            <pubDate>Sat, 29 May 2021 20:45:35 GMT</pubDate>
            <atom:updated>2022-06-14T17:15:16.753Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q-T7KT9J-5JplHXJ1nhPQg.png" /><figcaption>Monke Cafe</figcaption></figure><p>About a month ago, a friend and I came up with an idea for a small website and decided to create an MVP in a couple of days to give it a shot. The idea was pretty simple; a meme-driven chat room and a live price chart for each cryptocurrency. I was assigned to create the backend for the project, and my friend would make the web client.</p><p>I set three requirements for myself before starting to work on the server. First and foremost, I wanted to get the MVP out as soon as possible. Secondly, I wanted to make the server as lightweight as possible to just leave it running on a cheap VM. Lastly, I wanted to design the architecture to allow for easy scaling if the shit coin investors decide to make another stupid decision and use our application.</p><p>The first step was to think of the entire server as a pipeline. Basically, the whole thing is a pipeline that consumes a set of events, processes them, and then streams them to many clients.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q9nnGZs_yqZfPxam8buiow.png" /><figcaption>Pipeline Architecture</figcaption></figure><p>Any incoming data can be considered as some sort of event. For example, we have things like users joining or leaving a room, publishing or deleting comments, and ticker price updates. On the other side, anything that consumes these events can be considered as a client; our database, cache, and every connected browser.</p><p>After giving this a bit more thought, the first thing that came to my mind was a combination of <a href="https://rxjs-dev.firebaseapp.com/">RxJS</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events">Server-Sent Events</a>. I have never written a server-side javascript project as I mainly use Golang and Python, but I really wanted to experiment with RxJS on the server-side, and this seemed to be the perfect time to give it a shot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H08fb-5QAjCxXVn8o5DQyA.png" /><figcaption>Server-Sent Events (SSE) + RxJS</figcaption></figure><p>To explain the implementation in more detail, I will walk through implementing a very simple stock/cryptocurrency live price streaming endpoint. First, we need an <a href="https://rxjs-dev.firebaseapp.com/">RxJS</a> Subject which is a multicast observable. Our subject will take care of streaming the ticker price events to all of our clients (subscribers).</p><pre>import { Subject } from &#39;rxjs&#39;;<br>import { filter, map } from &#39;rxjs/operators&#39;;</pre><pre>// our RxJS subject<br>const TickerSubject = new Subject();</pre><p>Next, we need an entry point for our events to be pushed to our stream. We can either emit directly to our subject or create a wrapper function as an abstraction layer and sanitize and check our data before emitting new events.</p><p>To do so, let’s define a function called <strong><em>EmitTickerPrice. </em></strong>Every time we get a new ticker price data, we will call this method with the proper parameters, and it will emit a new event to our ticker subject.</p><pre>/**<br> * Emit a new ticker price<br> * <a href="http://twitter.com/param">@param</a> {string} symbol: ticker symbol<br> * <a href="http://twitter.com/param">@param</a> {string} price: ticker price<br> * <a href="http://twitter.com/param">@param</a> {string} currency: ticker currency<br> */<br>const EmitTickerPrice = async (symbol, price, currency) =&gt; {<br>    const ticker = {<br>        symbol, price, currency,<br>        createdAt: Math.floor(new Date() / 1000),<br>    };<br>    TickerSubject.next(ticker);<br>    return ticker;<br>};</pre><p>For our project, I am using PostgreSQL to persist historical ticker price information. I am also using Redis as a cache store to reduce the database load when clients request the data to render the price chart. As I mentioned previously, each of these is considered a client and independently subscribed to our <a href="https://rxjs-dev.firebaseapp.com/">RxJS</a> subjects. We can call subscribe on our ticker subject and pass a callback method to observe and handle each incoming event.</p><pre>TickerSubject.subscribe(ticker =&gt; {})</pre><p>Pretty easy, right? Well, not really. See, for our website, we are pushing a new ticker price event every five seconds for each supported stock and cryptocurrency ticker. These events are also not synchronized and come in at different intervals, which means that we get dozens of ticker events every second. The problem is that we don’t want to call our Redis and PostgreSQL subscriber callback every time a new event is emitted. Instead, we want to implement some additional logic in our pipeline to reduce the load on these services.</p><h4><strong>PostgreSQL Observer</strong></h4><p>Let’s start with PostgreSQL; inserting a new row individually each time a new ticker price is emitted is not ideal. This may differ for the different projects as, in some cases, we may need atomic inserts. However, for this project, the 30 seconds insertion delay was negligible. Luckily, <a href="https://rxjs-dev.firebaseapp.com/">RxJS</a> makes it very easy to implement this feature by providing pipelines and dozens of operators. For our case, we can create a pipe and use the <strong><em>bufferTime</em></strong> operator to buffer our events for 30,000 milliseconds. Then, we can subscribe to the newly defined pipeline.</p><pre>import { bufferTime } from &#39;rxjs/operators&#39;;</pre><pre>TickerSubject.TickerSubject.pipe(<br>    bufferTime(30000),<br>).subscribe(tickers =&gt; {})</pre><p>Our subscriber is called every 30 seconds, and it gets a list of buffered events in the past buffer period instead.</p><h4>Redis Observer</h4><p>Our problem gets a bit more interesting with Redis. As I mentioned previously, Redis is mainly used to cache the price points needed to generate the price chart displayed on the website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xrC4C1vjpyi3TuJ6.png" /><figcaption>Monke Cafe Price Chart</figcaption></figure><p>This chart is created for the different intervals such as the past 5 minutes, one hour, or a day. As you can tell by now, we don’t need a data point every 5 seconds for our 24-hour chart; instead, a data point every 30 minutes or even an hour would do the job.</p><p>Our Redis observer should throttle each unique ticker symbol for 30 minutes before calling the subscriber. To achieve this, we need to create a bit more complicated pipeline than what we previously had for the PostgreSQL observer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4cjkZuvUXEusrvXUyy3IgA.png" /></figure><p>First, we have to group our events based on their ticker symbol. To do so, we can use the <strong><em>groupBy</em></strong> operator provided by <a href="https://rxjs-dev.firebaseapp.com/">RxJS</a> and provide an arrow function to specify how we are grouping these events. We want the group our events based on their ticker symbols; hence, we return the ticker symbol value from our arrow function.</p><p>Next, we will throttle each group to emit once every 30 minutes and finally merge all the groups into a single pipeline. We can use the <strong><em>mergeMap</em></strong> operator and map through each group to add the <strong><em>throttleTime </em></strong>operator with a 30-minute interval. Finally, we can subscribe to the pipeline and insert the data into our Redis server.</p><pre>import { groupBy, mergeMap, throttleTime } from &#39;rxjs/operators&#39;;</pre><pre>TickerSubject.pipe(<br>    groupBy((ticker) =&gt; ticker.symbol),<br>    mergeMap((group) =&gt; group.pipe(<br>        throttleTime(30 * 60 * 1000),<br>    )),<br>).subscribe(ticker =&gt; {})</pre><p>We can even go further and buffer these events to take advantage of Redis pipelines, but I will skip that part as it will look almost identical to what we did with our PostgreSQL pipeline.</p><p>If you made it thus far, pat yourself on the back, take a deep breath and go get some coffee before we get our hands dirty with server-sent events.</p><h4>Server-Sent Events Endpoint</h4><p>For our website, I am using <a href="https://expressjs.com/">Express</a> and the <a href="https://www.npmjs.com/package/@awaitjs/express">@awaitjs/express</a> library to use async/await in my routers. Register the path <strong><em>/ticker/:symbol/event</em></strong> via GET method on our express server to create our server-sent events route.</p><pre>Router.getAsync(&#39;/ticker/:symbol/event&#39;, async (req, res) =&gt; {})</pre><p>To enable SSE, we need to flush a couple of headers back to our client. We want the <strong><em>‘Connection’</em></strong> set to <strong><em>‘keep-alive’</em></strong>, <strong><em>‘Cache-Control’</em></strong> set to <strong><em>‘no-cache’</em></strong> and <strong><em>‘Content-Type’ </em></strong>set<strong><em> </em></strong>to <strong><em>‘text/event-stream’</em></strong> so our client would understand that this is an SSE route.</p><p>In addition, I have added <strong><em>‘Access-Control-Allow-Origin’</em></strong> for CORS and <strong><em>‘X-Accel-Buffering’</em></strong> set to <strong><em>‘no’</em></strong> to avoid <a href="https://www.nginx.com/">Nginx</a> from messing with this route. Finally, we can flush the headers back to our client to kickstart the event stream.</p><pre>Router.getAsync(&#39;/ticker/:symbol/event&#39;,<br>    async (req, res) =&gt; {<br>        res.setHeader(&#39;Cache-Control&#39;, &#39;no-cache&#39;);<br>        res.setHeader(&#39;Content-Type&#39;, &#39;text/event-stream&#39;);<br>        res.setHeader(&#39;Connection&#39;, &#39;keep-alive&#39;);<br>        res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>        res.setHeader(&#39;X-Accel-Buffering&#39;, &#39;no&#39;);<br>        res.flushHeaders();<br>});</pre><p>We can now start streaming data by writing something into our response. SSE provides a text-based protocol that we can use to help our clients differentiate between the event types. Each one of our events should look like the following:</p><pre>event: ${event name}\n<br>data: ${event data}\n\n</pre><p>To make our lives a bit easier, I have created a helper function to take care of serialization for us.</p><pre>/**<br> * SSE message serializer<br> * <a href="http://twitter.com/param">@param</a> {string} event: Event name<br> * <a href="http://twitter.com/param">@param</a> {Object} data: Event data<br> * <a href="http://twitter.com/returns">@returns</a> {string}<br> */<br>const EventSerializer = (event, data) =&gt; {<br>    const jsonString = JSON.stringify(data);<br>    return `event: ${event}\ndata: ${jsonString}\n\n`;<br>};</pre><p>On our website, we have half a dozen subjects similar to what we have created so far. To be able to differentiate between these events, we have to assign an event name to each. Let’s use “<strong><em>price_update</em></strong>” for the ticker subject. In addition, we need to filter these events based on the dynamic path that our client has subscribed. For example, on <strong><em>/ticker/DOGE/event</em></strong>, we only want events related to Dogecoin. To implement these two features, let’s create a new wrapper around our ticker subject to filter the pipeline and add our event name to the events.</p><pre>import { filter, map } from &#39;rxjs/operators&#39;;</pre><pre>/**<br> * Event stream for ticker price update<br> * <a href="http://twitter.com/param">@param</a> {string} symbol: ticker symbol<br> * <a href="http://twitter.com/returns">@returns</a> {Observable&lt;{data: *, name: string}&gt;}<br> */<br>function EventTickerStream(symbol) {<br>   return TickerSubject<br>      .pipe(<br>         filter((ticker) =&gt; ticker.symbol === symbol),<br>         map((ticker) =&gt; {<br>            return { data: ticker, name: &#39;price_update&#39; };<br>         }<br>      ),<br>   );<br>}</pre><p>All left to do is merge these events into a single pipeline and create a new subscriber to write them into the SSE connection. We can take use the <strong><em>of</em></strong> operator to create a pipeline from all of our subjects. Then, we use the <strong><em>mergeAll</em></strong> operator to collect and merge all of our observables into a single observable. Then, we can subscribe to the observable, serialize our data and write it to our response. Finally, we have to make sure to unsubscribe from our observer when the SSE connection is closed. Putting all of these together, we should have something like the following</p><pre>import { of } from &#39;rxjs&#39;;<br>import { mergeAll } from &#39;rxjs/operators&#39;;</pre><pre>Router.getAsync(&#39;/ticker/:symbol/event&#39;,<br>    async (req, res) =&gt; {</pre><pre>        res.setHeader(&#39;Cache-Control&#39;, &#39;no-cache&#39;);<br>        res.setHeader(&#39;Content-Type&#39;, &#39;text/event-stream&#39;);<br>        res.setHeader(&#39;Connection&#39;, &#39;keep-alive&#39;);<br>        res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>        res.setHeader(&#39;X-Accel-Buffering&#39;, &#39;no&#39;);<br>        res.flushHeaders();</pre><pre>        const symbol = req.params.symbol.toUpperCase();</pre><pre>        const stream$ = of(<br>            EventTickerStream(symbol),<br>            // other events ...<br>        ).pipe(<br>            mergeAll(),<br>        ).subscribe((event) =&gt; {<br>            res.write(EventSerializer(event.name, event.data));<br>        });</pre><pre>        req.on(&#39;close&#39;, () =&gt; {<br>            stream$.unsubscribe();<br>        });<br>});</pre><p>Aaannddd… that’s it! We are done with our backend server. <br>Here’s an overall view of what we have created so far.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8YN4_jsUOIr0OGnSAZETXg.png" /><figcaption>RxJS and Server-Sent Events (SSE) Backend Architecture</figcaption></figure><h4>Server-Sent Events Client</h4><p>To subscribe to our SSE route, we can create a new instance of the EventSource interface and pass our endpoint to the constructor. Once we have an instance, we can add event handlers for specific event names to process the incoming data. In our case, we can subscribe to the <strong><em>“price_update”</em></strong> event for Dogecoin and use the data to update our UI.</p><pre>const eventSource = new EventSource(&quot;/ticker/DOGE/event&quot;);</pre><pre>eventSource.addEventListener(<br>   &quot;price_update&quot;, (event) =&gt; {<br>       const data = JSON.parse(event.data);<br>       // use the data to update the UI<br>    }, false<br>);</pre><pre>// close the connection when needed<br>eventSource.close();</pre><p>At the end of the day, I am pleased about this architecture as it satisfies most of my requirements for this project. Going with a reactive design allowed me to implement many complex features more efficiently and less error-prone than an imperative model. Higher-level functions provided by RxJS, such as throttleTime and bufferTime solved many of my problems very quickly and saved me a lot of development time. Completing the first iteration of the MVP took us about 4 days.</p><p>I also wanted to deploy our services on the smallest virtual machine to reduce costs and benchmark server-side performance. Thus, I went with the $5/month digital ocean droplet. Over the last week, our server has served over 3.7M requests and over 120M events, and at one point, we had over 500 concurrent clients, which I think is a pretty damn good benchmark.</p><p>In terms of scalability, we still have a lot of room to grow vertically and increase the resources available on the VM. But if we want to grow horizontally, the current architecture allows us to deploy proxies that subscribe to our pipelines, either through our SSE endpoint or to the subjects over the network, and then multiplex the events to more clients.</p><p>That concludes the discussion on how I implemented an event-driven server for our project.</p><p>Thank you for reading; if you would like to chat, you can find me on Twitter <a href="https://twitter.com/ImSh4yy">@imsh4yy</a> or via responses here.</p><p>Update: I have recently started working on a new project and have been using the same architecture design for pushing down information to my users. I would love to hear your feedback on the project: <a href="https://logsnag.com/">logsnag.com</a></p><p><em>More content at </em><a href="http://plainenglish.io/"><em>plainenglish.io</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f8be1ffc123" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/how-i-created-an-event-driven-backend-with-rxjs-server-sent-events-and-expressjs-9f8be1ffc123">How I Created an Event-Driven Backend with RxJS, Server-Sent Events, Express, and Node.js</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What Bottly taught me]]></title>
            <link>https://sh4yan.medium.com/what-bottly-thought-me-3b2608efdda5?source=rss-e50b596f6f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b2608efdda5</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[apps]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[apple]]></category>
            <dc:creator><![CDATA[Shayan]]></dc:creator>
            <pubDate>Mon, 13 Feb 2017 20:41:44 GMT</pubDate>
            <atom:updated>2017-02-14T16:49:57.951Z</atom:updated>
            <content:encoded><![CDATA[<p>It was a couple of months ago when I decided to create and publish an app to the Appstore as a side project. This was mainly because of two reasons; first because I was bored, and second was because I was paying for the Apple developer account, which is $100 a year, and I wasn’t using it.</p><p>I sat down and started thinking about a simple app that would not take a long time to develop. Also, I wanted to use different technologies such as <a href="http://vapor.codes/">Vapor</a>, <a href="http://mongokitten.openkitten.org/">MongoDB</a>, etc. So I finally decided to make a modern version of sending messages inside of bottles to the sea in the hope of someone finding it and letting us know when and where it ended up based on the information on the message. I designed the UI which took me a couple of hours, and I started thinking that it might be a stupid idea to waste time on this app. I saved the sketch file somewhere on my computer and stopped thinking about it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E97M-8CfPptxbGX--slQvQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*94cS_Jt5QJaPvzn4_O3-Tg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UbYAWYO8YE3rkaUKRClO_w.png" /></figure><p>Last week, I was going through my files cleaning the hard drive, and I stumble upon the sketch files. I thought that maybe it might not be that bad to spend a weekend on it and release it just for fun. So I did. I spent a weekend and a Monday on it and finished the app. I had an exam on that Thursday, so I had to wait until Thursday night to complete the details and submit the app to the app store for review. I managed to send it to the app store at around 11 pm on Thursday and was expecting a week for it to be accepted if not rejected.</p><p>I built a small swift <a href="https://github.com/Sh4yy/Telegram-Logging">library</a> which sends logs to my Telegram account using telegram bots. I use this library on the server for important logs such as errors, or new downloads. It was around 12:30 pm, I was sitting at this place called Pho 75, (shoutout to that number 12, large bowl with extra jalapeños), and I received a notification from telegram saying someone had downloaded the app. So at first, I thought that it might be a bug or something wrong with the server, however, it was a legit download by the person who was reviewing the app. After 20 minutes, I received another notification, this time from iTunes Connect app, saying the app was pending developer release.</p><p>Later on that day, I was going through the database removing all the fake bottles I made for test and I came across this,</p><blockquote>“Hello</blockquote><blockquote>Hope u r having a great day!</blockquote><blockquote>Peace,”</blockquote><p>This is the first bottle sent to the game by a user, the guy who reviewed my app at Apple. (If you are reading this, I hope you had a great day as well).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*8EdZV2_SCBCg9mI_LK_rtw.gif" /></figure><p>Now it was the time to publish the <a href="https://shayy.org/bottly">app</a> to the app store. I searched around to find when is the best time of the day and week to release an app on the app store, and it seems like people say it’s Friday night because of the weekend. Luckily, it was Friday. I waited until 5 pm and published the app. I wasn’t really expecting anything except people downloading the app and sending random texts or cursing; I have to mention that is the main reason why I set the age limit to 17+, but nothing really happened that night. I got only two downloads, one of them was from me and the other was from my friend.</p><p>I slept, and the morning after, I turned on my phone (yes I do turn it off during the night) and I saw that I have about 30 notifications from telegram (which was surprising). All of them were download notifications and luckily no bugs or errors. I checked the database, and there were about 150 bottles sent that night so I read a couple of them. I was expecting from the beginning of this journey, that people will only use the app to roast each other, but it was quite the opposite, there were a lot of nice messages sent, complimenting the app’s idea and its UI, wishing the person who received the message a good day, greetings from different countries. There even was an in-depth conversation between the first two dudes who installed the app after my friend and me.</p><blockquote>“Remember you are you and that is amazing! Find a way today to make someone’s day brighter maybe a smile to someone or give a hug.”</blockquote><blockquote>“Hello stranger!<br>In Canada it’s very cold at the moment and the water is mostly frozen, if this bottle reaches you, it’s a miracle!”</blockquote><blockquote>“Starting a new job tomorrow. Wish me luck!”</blockquote><blockquote>“I’m writing this to remind whoever is reading this if u love someone, tell them, show them, prove them, love is never too late..”</blockquote><p>This was the first thing I learned from this process: your expectations might be totally wrong.</p><p>I noticed two small problems with the app. One was an issue with some notifications not being sent, and I also wanted to add a blocking mechanism in case someone starts to mess with the app. Both only took me about an hour to complete.</p><p>And this is another thing I learned creating Bottly: Make the important decisions on the server, and keep the client as dynamic as possible. This way, updating or changing something will only take you a couple of hours, instead of repeating the whole process of submitting the app to the app store.</p><p>The day went on, and I got another 160 downloads without doing any advertising. I was also using the app and finding bottles, when something got my attention; there was a huge range of users from all around the planet. I received bottles from Australia, Japan, China, South Africa. Only 20 hours after I released the app, there were people thousands of miles away from me using it to send each other messages in bottles. We had never known or met each other, or we might never even meet in the future, but the app was connecting us together. We were on different sides of the planet, but it only took a fraction of a second to say something to someone new.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*zOZ_jI-Fag7UfMY_zUe9cQ.gif" /><figcaption><a href="http://gph.is/1kw5oF6">http://gph.is/1kw5oF6</a></figcaption></figure><p>Consequently, I decided to set up a <a href="https://shayy.org/map">web page</a> on my personal website, to show where bottles are being sent and received on the map. I rounded the longitude and the latitude of the users’ location to one decimal point to cause a 100km/60mi error and maintain their privacy. I then created an algorithm to fetch all of these coordinates from the database and show them on a map. It resulted in an amazing data visualization; annotations around the earth, showing that my Weekend Project was being used in different countries and continents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s_PozfLLBjI9SxUZzLbkgA.png" /><figcaption>https://shayy.org/map</figcaption></figure><p>After all of the positive feedbacks I received, I have decided to add a messaging system to the app on the next update to let people chat with the individual who sent a bottle to them. Using this feature, people can continue their conversation and potentially make new friends from different region (who doesn’t want friends from different parts the world?)</p><p>Thank you for reading my Bottly story,<br>I would really appreciate if you send me your feedback about the application.</p><p>Here is a link to download the app <a href="https://shayy.org/bottly">https://shayy.org/bottly</a><br>and this is a link to the map <a href="https://shayy.org/map">https://shayy.org/map</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b2608efdda5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>