<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: readwarn</title>
    <description>The latest articles on DEV Community by readwarn (@readwarn).</description>
    <link>https://dev.to/readwarn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F468617%2F5b88814d-8394-448d-9048-337df4127141.jpeg</url>
      <title>DEV Community: readwarn</title>
      <link>https://dev.to/readwarn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/readwarn"/>
    <language>en</language>
    <item>
      <title>A debounced function call with JavaScript in a vue app</title>
      <dc:creator>readwarn</dc:creator>
      <pubDate>Wed, 22 Jun 2022 22:00:05 +0000</pubDate>
      <link>https://dev.to/readwarn/a-debounced-function-call-with-javascript-in-a-vue-app-3f69</link>
      <guid>https://dev.to/readwarn/a-debounced-function-call-with-javascript-in-a-vue-app-3f69</guid>
      <description>&lt;p&gt;Here I want to talk about the concept of debounce, or rather something really similar to it. &lt;/p&gt;

&lt;p&gt;So I was faced with the need to limit the number of api calls I make to an endpoint, as the endpoint has a middleware that limits the amount of calls you can make to it. (a call per second for each client, for any api calls more than two per second, it throws an error). So if you also ever need to use this kind of api, this post will be helpful. &lt;/p&gt;

&lt;p&gt;The solution is rather simple thanks to the JavaScript setTimeout function. So based on the restriction set by this api, I needed a way to control the frequency of the api calls to be at most one per second.&lt;/p&gt;

&lt;p&gt;Do note, I needed to call this api to implement a search functionality. This means that a user of the app will be calling the api per keystrokes (as they type to search). This resulted in the api getting called frequently as an average user type more than one character per second. &lt;/p&gt;

&lt;p&gt;The summary of solution to this is “for every api request a user make, check if there was any request made less than a second ago. If there is, “defer” the new api request to a second later, else call the api immediately”. &lt;/p&gt;

&lt;p&gt;The solution will be tailored to vuejs app but it can also be used on other stack. &lt;/p&gt;

&lt;p&gt;To implement this solution, the following will be needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;input=&lt;/span&gt;&lt;span class="s"&gt;"handleSearch"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"search"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;can_start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;search_timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;
    &lt;span class="na"&gt;can_start_timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resetCanStart&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;yourThrottledApi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// the api endpoint with request limit of 1 &lt;/span&gt;
    &lt;span class="c1"&gt;// request per second&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nx"&gt;deferredSearch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search_timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search_timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// this delete pending api request, so we don't have more &lt;/span&gt;
    &lt;span class="c1"&gt;// than one pending request queued&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; 
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="c1"&gt;// create a pending request which is called after a delay&lt;/span&gt;

  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nx"&gt;handleSearch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;can_start&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deferredSearch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// this handles the search such that search api request is &lt;/span&gt;
    &lt;span class="c1"&gt;// called immediately if 'can_start' is true, else the&lt;/span&gt;
    &lt;span class="c1"&gt;// request is delayed for a second&lt;/span&gt;

  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nx"&gt;resetCanStart&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;can_start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;can_start_timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;can_start_timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;can_start_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;can_start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// this set 'can_start' to false immediately after a request, &lt;/span&gt;
    &lt;span class="c1"&gt;// and then set it back to true after a delay (1 second) &lt;/span&gt;
    &lt;span class="c1"&gt;// (i.e it blocks another request from being made by setting &lt;/span&gt;
    &lt;span class="c1"&gt;// 'can_start' to false, then allows new request only after 1 &lt;/span&gt;
    &lt;span class="c1"&gt;//  second by setting 'can_start' to true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Implications of the solution &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;For instance where the api returns the result in less than a second (say T second), consequent api calls made immediately will be forcibly delayed for some moment i.e (1 second - T second).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Some api request won't be made if both of these conditions are true&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; the current request is made less then a second after the last request&lt;/li&gt;
&lt;li&gt; another request is made less than a second after the current request&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A chat app inspired by whatsapp</title>
      <dc:creator>readwarn</dc:creator>
      <pubDate>Sat, 04 Dec 2021 19:43:29 +0000</pubDate>
      <link>https://dev.to/readwarn/a-chat-app-inspired-by-whatsapp-3hc9</link>
      <guid>https://dev.to/readwarn/a-chat-app-inspired-by-whatsapp-3hc9</guid>
      <description>&lt;ul&gt;
&lt;li&gt;App link: &lt;a href="https://ecstatic-hugle-48075c.netlify.app/"&gt;Chat live-link&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Repo link: &lt;a href="https://github.com/readwarn/single-page-chat-app"&gt;Chat repo&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Stack : MEVN&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hi guys, so yet again, I created a chat app, but this time, the app flow was inspired by WhatsApp. &lt;/p&gt;

&lt;p&gt;Yea, this is the second chat-app project I've created, the first being a &lt;a href="https://calm-meadow-71961.herokuapp.com/login"&gt;discord-like app&lt;/a&gt;. &lt;a href="https://dev.to/readwarn/a-chat-app-i-made-using-mevn-stack-and-socket-io-2h3a"&gt;Here&lt;/a&gt; is article I wrote about it.&lt;/p&gt;

&lt;p&gt;So this app basically let user sign up with just a unique username (no password needed). After signup, all new users are automatically added to the chat &lt;strong&gt;welcome channel&lt;/strong&gt;. This channel is for new user to introduce themselves and interact with existing users.&lt;/p&gt;

&lt;p&gt;Also the app has a &lt;strong&gt;DM feature&lt;/strong&gt;, which allows users to chat privately. To initiate the private chat with a user, all you need to do is send them a &lt;strong&gt;friend request&lt;/strong&gt;, as soon as they accept the request, you can start to chat with them.&lt;/p&gt;

&lt;p&gt;I really don't want to explain how the whole app works or how to navigate through it. I just want to be sure the app is intuitive enough for user to understand and use. &lt;/p&gt;

&lt;p&gt;I will also love to hear your feedbacks and feature suggestions. Thanks&lt;/p&gt;

&lt;p&gt;PS: This app was created in less than a day [Vue.js sure makes everything simple]. &lt;/p&gt;

&lt;p&gt;Also I am really really open to a full time role/gig as a Vue.js dev. You can reach me on my email: &lt;a href="mailto:akinyusuf5@gmail.com"&gt;akinyusuf5@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>My goals for the HNGi8 Internship</title>
      <dc:creator>readwarn</dc:creator>
      <pubDate>Sun, 15 Aug 2021 22:06:53 +0000</pubDate>
      <link>https://dev.to/readwarn/my-goals-for-the-hngi8-internship-38nh</link>
      <guid>https://dev.to/readwarn/my-goals-for-the-hngi8-internship-38nh</guid>
      <description>&lt;p&gt;Here comes another edition of the hng internship [&lt;a href="https://zuri.team"&gt;https://zuri.team&lt;/a&gt;]. This will be be my second internship in two years, having participated in the last year, hngi7 edition. I've set a goal for myself for the 8-week remote internship. &lt;/p&gt;

&lt;p&gt;This article is my submission for the stage one task, stating my objective at the end of the internship.&lt;/p&gt;

&lt;p&gt;At the end of the internship, I wish to become a better communicator and improve my team-work ability. &lt;/p&gt;

&lt;p&gt;I've been a developer for close to two years, and the hgni7 internship was part of my grooming process. The internship is a key part of my learning process, introducing me to "tech community" and valuable resources. Over the years as a developer, I've come to realize being a good 'coder' isn't always enough to thrive in the tech ecosystem. To become a successful developer, you need to be an excellent communicator, one who can express himself clearly. &lt;/p&gt;

&lt;p&gt;As such, I wish to work on my communication skills, and on my social skills in general. I hope to be able to manage a team of developers and deliver a task/project.&lt;/p&gt;

&lt;p&gt;Check this figma tutorial, &lt;a href="https://trydesignlab.com/figma-101-course/introduction-to-figma/"&gt;https://trydesignlab.com/figma-101-course/introduction-to-figma/&lt;/a&gt;,  if you are just getting started as a designer. &lt;/p&gt;

&lt;p&gt;To get started and familiar with GIT, check this tutorial, &lt;a href="https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners"&gt;https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get started with Javascript, you can take a look at this tutorial &lt;a href="https://www.tutorialspoint.com/javascript/index.htm"&gt;https://www.tutorialspoint.com/javascript/index.htm&lt;/a&gt;  &lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Chat app I made using MEVN stack and socket.io</title>
      <dc:creator>readwarn</dc:creator>
      <pubDate>Tue, 30 Mar 2021 18:09:53 +0000</pubDate>
      <link>https://dev.to/readwarn/a-chat-app-i-made-using-mevn-stack-and-socket-io-2h3a</link>
      <guid>https://dev.to/readwarn/a-chat-app-i-made-using-mevn-stack-and-socket-io-2h3a</guid>
      <description>&lt;p&gt;After months of procrastination, I'm finally writing my first article as a developer&lt;br&gt;
So I made a &lt;a href="https://calm-meadow-71961.herokuapp.com/login" rel="noopener noreferrer"&gt;chat app&lt;/a&gt; with the MEVN (MongoDB, Express, Vue.js, Node.js) stack and &lt;a href="https://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt;. The app functionalities are inspired by discord app. &lt;/p&gt;
&lt;h4&gt;
  
  
  The app features include:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;User can login with a username and password or using other social media accounts&lt;/li&gt;
&lt;li&gt;Users are automatically added to a welcome channel&lt;/li&gt;
&lt;li&gt;Users can create a channel&lt;/li&gt;
&lt;li&gt;Users can search and join a channel&lt;/li&gt;
&lt;li&gt;Users can leave a channel&lt;/li&gt;
&lt;li&gt;Users can edit their profile and change their display picture&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Features I intend to add later
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Users can browse through and join public channels&lt;/li&gt;
&lt;li&gt;Users can directly message members of a channel&lt;/li&gt;
&lt;li&gt;A simple profile card of a user is displayed on hovering over his/her username&lt;/li&gt;
&lt;li&gt;User can change the theme of the app.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post will focus on how &lt;a href="https://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt; is used on the app. Here is the &lt;a href="https://github.com/readwarn/Real-time-chat-app" rel="noopener noreferrer"&gt;Link&lt;/a&gt; to the source code, and the &lt;a href="https://calm-meadow-71961.herokuapp.com/login" rel="noopener noreferrer"&gt;live link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;However, to get a clear understanding of this post, I'll explain few key terms.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The channel schema&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose=require('mongoose');
const Schema=mongoose.Schema;
const channelSchema=new Schema({
    name:String,
    description:String,
    members:[
        {
            type:mongoose.Schema.Types.ObjectId,
            ref:"User"
        }
    ],
    messages:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:"Message"
    }]
})

module.exports=mongoose.model('Channel',channelSchema);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;When a user sends message to a channel, the channel is updated by pushing the new message to the channel's messages array.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Channel&lt;/strong&gt;: This represents the channel being currently viewed by a user&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbgavxjoj9ay6uqjfbbr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbgavxjoj9ay6uqjfbbr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Channels&lt;/strong&gt;: This represents the array of channels a user belongs to.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4hp1dqidaqjpaejyeb67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4hp1dqidaqjpaejyeb67.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Updated Channel&lt;/strong&gt;: This is a channel object updated with latest messages sent by members. i.e. new messages are already pushed to its &lt;em&gt;messages&lt;/em&gt; array properties.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  SOCKET.IO
&lt;/h3&gt;

&lt;p&gt;The socket.io was used to allow for real-time bidirectional data flow between the client and the server. i.e. the client can emit event, along with some data, while the server listens to this event and handles them accordingly. This means that data can be exchanged between the client and the server without having to refresh the page.&lt;/p&gt;
&lt;h4&gt;
  
  
  Socket.io on the server
&lt;/h4&gt;

&lt;p&gt;The socket.io is installed as shown&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install socket.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The socket io connection is then set up with express server as shown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express=require('express');
const socket = require('socket.io');
const app=express();
const server = app.listen(process.env.PORT || 3000,function(){
  console.log("running");
});

const io = socket(server,{
  cors: {
    origin: "https://calm-meadow-71961.herokuapp.com",
    methods: ["GET","PUT", "POST"],
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the server side, the socket listens to three kinds of event emitted from the client side.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RegisterAll&lt;/strong&gt;: This event is emitted whenever a client connects to the socket.io connection. The user's &lt;strong&gt;channels&lt;/strong&gt; array is sent along with the event. This event is handled by &lt;em&gt;subscribing&lt;/em&gt; the client to each &lt;strong&gt;channel's&lt;/strong&gt; ID in the &lt;strong&gt;channels&lt;/strong&gt; array i.e. the client joins an array of channel's ID &lt;a href="https://socket.io/docs/v4/rooms/" rel="noopener noreferrer"&gt;room&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;socket.on('registerAll',channels=&amp;gt;{
      channels.forEach(channel =&amp;gt; {
         socket.join(channel._id);
      });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Register&lt;/strong&gt;: This event is emitted when a user joins a new channel. The new &lt;strong&gt;channel&lt;/strong&gt; object is sent along with the event. This event is handled by &lt;em&gt;subscribing&lt;/em&gt; the client to the new &lt;strong&gt;channel's&lt;/strong&gt; ID i.e. the client joins the new room.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; socket.on('register',channel=&amp;gt;{
       socket.join(channel._id);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;messageSent&lt;/strong&gt;: This event is emitted along with updatedChannel object when, 

&lt;ul&gt;
&lt;li&gt;Message is sent to a channel; &lt;/li&gt;
&lt;li&gt;User leaves a channel; &lt;/li&gt;
&lt;li&gt;User joins a channel; &lt;/li&gt;
&lt;li&gt;User creates a new channel.
This event is handled by emitting the &lt;strong&gt;messageRecieved&lt;/strong&gt; event along with the updatedChannel object to all clients that belong to the channel's ID room.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; socket.on('messageSent',(channel)=&amp;gt;{
       socket.to(channel._id).emit('messageReceived',channel);
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: The &lt;strong&gt;messageSent&lt;/strong&gt; and the &lt;strong&gt;Register&lt;/strong&gt; events are both emitted from the client when a user &lt;strong&gt;joins&lt;/strong&gt; a channel or &lt;strong&gt;creates&lt;/strong&gt; a new channel&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Socket.io on the client
&lt;/h4&gt;

&lt;p&gt;Socket.io is installed on the client side&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install socket.io-client;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is then imported and initialized with the express server url&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
import io from 'socket.io-client';
data(){
  return {
   disconnected:false,
   channels:[],
   channel:{},
   socket:io("https://whispering-everglades42925.herokuapp.com"),
  }
}
&amp;lt;script/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the client side, the client handles three events emitted from the server side.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;connect&lt;/strong&gt;: This is a reserved event emitted when a client connects to the socket.io connection. The event is handled by emitting the 'regsiterAll' event along with the user's &lt;strong&gt;channels&lt;/strong&gt; [the array of channels the user belongs to] and also setting the disconnected variable to false.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.socket.on("connect", () =&amp;gt; {
     if(this.channels.length&amp;gt;0){
          this.registerChannels(this.channels);
          this.disconnected=false;
     }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;disconnect&lt;/strong&gt;: This is a reserved event emitted when a client is disconnected from the socket.io connection. This event is handled by setting the &lt;em&gt;disconnected&lt;/em&gt; variable to true.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.socket.on("disconnect", (reason) =&amp;gt; {
      this.disconnected=true;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence the paragraph, "disconnected", is displayed whenever the client is disconnected from the socket connection&lt;br&gt;
&lt;code&gt;&amp;lt;p class="disconnected" v-if="disconnected"&amp;gt;disconnected&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;messageRecieved&lt;/strong&gt;: This event is handled by replacing a channel in the array of channels with the updated channel sent with the event.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; this.socket.on('messageReceived',(channel)=&amp;gt;{
       this.updateChannel(channel);
});`

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The updateChannel method is defined as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;funtion updateChannel(updatedChannel){
             if(updatedChannel._id===this.channel._id){
                 this.channel=updatedChannel;
             }
             this.channels = this.channels.map(channel =&amp;gt; (channel._id === updatedChannel._id) ? updatedChannel : channel)
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function takes the updatedChannel argument passed with the &lt;strong&gt;messageReceived&lt;/strong&gt; event, sets the currently viewed channel &lt;br&gt;
 to the updated channel if it is the same as the updatedChannel, then replace the outdated channel in the user's &lt;strong&gt;channels&lt;/strong&gt; with the updatedChannel&lt;/p&gt;

&lt;p&gt;There goes my first post, Thanks for reading.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>node</category>
      <category>mongodb</category>
      <category>socket</category>
    </item>
  </channel>
</rss>
