<?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 Funsho on Medium]]></title>
        <description><![CDATA[Stories by Funsho on Medium]]></description>
        <link>https://medium.com/@devfunsho?source=rss-bacef8d8fdc4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GGHljYe5IdbiUYaV.jpg</url>
            <title>Stories by Funsho on Medium</title>
            <link>https://medium.com/@devfunsho?source=rss-bacef8d8fdc4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 21:41:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@devfunsho/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[Happiness is not contagious]]></title>
            <link>https://medium.com/@devfunsho/happiness-is-not-contagious-75ec394f2c3b?source=rss-bacef8d8fdc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/75ec394f2c3b</guid>
            <category><![CDATA[happiness]]></category>
            <category><![CDATA[depression]]></category>
            <dc:creator><![CDATA[Funsho]]></dc:creator>
            <pubDate>Sun, 14 Oct 2018 17:11:07 GMT</pubDate>
            <atom:updated>2018-10-14T17:12:28.281Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>I naively thought it was, until I couldn’t!</blockquote><p>I had always told myself that all I needed to do when I’m not happy, is hang around people that are happy and can put a smile on my face. I could almost be in tears on my bed, and I would grab my car keys and drive to my friend’s place, hoping to find that happiness. It worked for a while, or I thought it did!</p><p>The lie we tell ourselves is that things will get better, often times, it feels that way. However, when you get to be in your true self, all alone with your flaws, your mistakes, bad decisions, we begin to drown. It is at this point, you then realize that what you thought was happiness, was merely temporary feeling to help you cope at that time.</p><p>What is true happiness and is it really achievable? To be honest, I don’t know. However, I have come to agree with myself that;</p><ul><li>It is okay not to be happy always</li><li>You can’t inherit happiness from others</li><li>Those friends, family, stranger, might not have the answers</li></ul><p>What is the way forward? Well, that’s where it gets complex and that’s why I have started a journey to discover myself. I need to know what makes me unhappy, how can I manage my emotions and how to find my long lasting happiness.</p><p>If you have gone through this before it going through one at the moment, you can share your story with me and how you are coping.</p><p>Gracias!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75ec394f2c3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Replacing Bootstrap 3 with 4 in your Laravel app]]></title>
            <link>https://medium.com/@devfunsho/replacing-bootstrap-3-with-4-in-your-laravel-app-1c2f393733a6?source=rss-bacef8d8fdc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c2f393733a6</guid>
            <category><![CDATA[bootstrap]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[bootstrap-4]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[laravel-5]]></category>
            <dc:creator><![CDATA[Funsho]]></dc:creator>
            <pubDate>Fri, 02 Feb 2018 06:58:37 GMT</pubDate>
            <atom:updated>2018-02-02T06:58:37.696Z</atom:updated>
            <content:encoded><![CDATA[<p>Yaay! Bootstrap 4 is finally here!</p><p><strong>TL;DR:</strong> If you are a web developer and you don’t know what Bootstrap is, hide your face!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3o6Zt1ffH0917qdI9a%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3o6Zt1ffH0917qdI9a%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3o6Zt1ffH0917qdI9a%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/e398b77ec7e3e4388462466f45ed45f8/href">https://medium.com/media/e398b77ec7e3e4388462466f45ed45f8/href</a></iframe><p>I will not be focusing on what has changed in Bootstrap 4, however, my friend <a href="https://medium.com/u/9e13edbcb55f">Prosper Otemuyiwa</a> wrote an awesome article about it <a href="https://auth0.com/blog/whats-new-in-bootstrap4/">here</a></p><p>It will literally take you just five minutes to make this change, you can be using Bootstrap 3 on first floor and by the time you get to third floor, you are enjoying Bootstrap 4.</p><p>Let’s dive into it!</p><h4>Modify package.json</h4><p>Here, we need to replace the old Bootstrap with version 4 and add <em>popper.js</em>. Your <em>package.json</em> should look like below when you are done.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f11150f5899d39d076663976fabf62db/href">https://medium.com/media/f11150f5899d39d076663976fabf62db/href</a></iframe><p>Once you are done, you can run “<em>npm install”</em>. This will remove <em>bootstrap-sass</em> and install bootstrap 4 and popper.js</p><h4>Modify bootstrap.js</h4><p>This file can be found at <strong><em>resources/assets/js</em></strong> folder. Here we need to make sure that we import <em>popper.js</em> first and then Bootstrap 4. Make sure your <em>bootstrap.js</em> file looks like below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d26ccfb587a74eecf4c22d9cfec72e91/href">https://medium.com/media/d26ccfb587a74eecf4c22d9cfec72e91/href</a></iframe><h4>Modify app.scss</h4><p>This file can be found at <strong><em>resources/assets/sass</em></strong> folder. This file imports the Bootstrap SASS files and we need to ensure we are importing that of Bootstrap 4. You can replace your <em>app.scss</em> with the one below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f6d6511b01661a0ecbe3835078d3ff5/href">https://medium.com/media/3f6d6511b01661a0ecbe3835078d3ff5/href</a></iframe><h4>Finally, run, run, run!</h4><p>Wait! There is no fire on the mountain, we just need to bundle the new Bootstrap 4 and other JavaScript packages in <em>bootstrap.js</em>, using the awesome Laravel Mix. Simply run “<em>npm run dev”</em> or “<em>npm run prod”</em> in your terminal, depending on your environment.</p><p>You can now enjoy the awesomeness of Bootstrap 4, simply add <strong><em>public/css/app.css</em></strong> and <strong><em>public/js/app.js</em></strong> to your Blade templates</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3o6ZsSEhYdsQOKZnAQ%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3o6ZsSEhYdsQOKZnAQ%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3o6ZsSEhYdsQOKZnAQ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/d20e68050c910927d0810a48fd04a2d1/href">https://medium.com/media/d20e68050c910927d0810a48fd04a2d1/href</a></iframe><p><strong><em>Muchas gracias!</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c2f393733a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a chat room in 30 minutes using Redis, Socket.io and Express]]></title>
            <link>https://medium.com/manifoldco/building-a-chat-room-in-30-minutes-using-redis-socket-io-and-express-9e8e5a578675?source=rss-bacef8d8fdc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e8e5a578675</guid>
            <category><![CDATA[socketio]]></category>
            <category><![CDATA[chat-room]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Funsho]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 20:26:03 GMT</pubDate>
            <atom:updated>2018-12-21T16:57:57.660Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WvD-qL20ChMSYUEb9FUVFg.png" /></figure><p>You are relaxing and listening to music in your office, then all of a sudden your desk phone rings, your boss hurriedly says “We need a chat room built and ready in less than 30 minutes to discuss a new project we are going to work on soon”. As the only developer around, you said “Ready in a bit!” with no idea about how to build one.</p><p>Don’t worry, get ready to build a simple chat room using <a href="http://manifold.co/">Manifold</a>, <a href="https://expressjs.com/">Express</a>, <a href="https://socket.io/docs/#">Socket.io</a> and <a href="https://zeit.co/">Zeit</a>. This tutorial will show you how easy it is to use one of Redis’ awesome feature called pub/Sub with Socket.io, to send and receive messages. Time to get to the action!</p><h4>Quick Intro on Redis</h4><p>Redis, which means <strong>REmote DIctionary Server</strong>, is an open source, in-memory data structure store. It can be used as a key-value database, cache and message broker. It supports a wide range of data structures such as strings, hashes, sets, lists etc. It also has on-disk persistence, which can be achieved by regularly dumping data to the disk or by appending commands to a log.</p><p>However, we are interested in one of Redis feature called Pub/Sub. Redis Pub/Sub allows a publisher (sender) send a message to a channel without knowing if there is any interested subscriber (receiver). Also, a subscriber expresses interest in a channel to receive messages without any knowledge of a publisher. Basically, a publisher is like a satellite antenna that sends out messages into deep space without knowing if aliens exist to receive the messages, while a subscriber is like a base station on Earth listening on a radio channel hoping to receive a message from deep space, without knowing if aliens are broadcasting.</p><p>Publishers and Subscribers are decoupled to make the process very fast and improve scalability since both subscribers and publishers are not aware of each other.</p><p><strong>Pros of using Redis PubSub</strong></p><ul><li>It is very fast since it makes use of in-memory data stores.</li><li>Slow subscribers can not delay publishers from broadcasting messages since it is not queue based.</li><li>The simplicity allows users to be flexible and easily scale applications</li></ul><p><strong>Cons of using Redis PubSub</strong></p><ul><li>It is not capable of persistence, which means messages are not saved or cached. Once a subscriber misses a message, there is no way it can get the message again. However, there are measures you can put in place to compensate, as we will see later in this tutorial.</li></ul><p>You can read up on Redis <a href="https://redis.io/">here</a>.</p><h4>Set up</h4><p>First, we need to set up Redis. Instead of spending time installing and configuring Redis on your server, you can head over to Manifold and create an instance. If you don’t have a Manifold account, you can <a href="https://dashboard.manifold.co/register">quickly create one</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uBEIt25uWvmcd2Qh-T5fnQ.png" /></figure><p>Once you are logged in, create a new project and provision a RedisGreen resource, this shouldn’t take very much time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rHjjoOGsgI3lzly2pA0pdg.png" /></figure><p>Click on Download .env button once the resource has been created, to download the .env file containing the credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bbcb8kQuNrjaL8QHogQABA.png" /></figure><p>Copy the downloaded .env file and paste it in your project root directory.</p><h3>Install modules</h3><p>To get started, we are going to install some node modules to get the chat room ready quickly. Ensure you have Node and NPM installed, then open your command line or terminal and run this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ddd575a6aec981ba5846e6fb8e6e66e/href">https://medium.com/media/1ddd575a6aec981ba5846e6fb8e6e66e/href</a></iframe><p>The command above will install ExpressJS framework, Redis client, Bluebird to promisify the Redis client, Socket.io, Pug as view template engine, node-env-file to configure environment file (.env) and body-parser to parse body requests, especially for POST methods.</p><p>Also, we are going to use Babel to transpile our JavaScript from ES6.</p><p>Quick Note: Socket.io allows real-time communication among clients. We will use it to send event-based messages between the web clients and the server.</p><h3>Setting up Redis</h3><p>Create a folder lib in your project root folder and create a file named redis.js, i.e lib/redis.js and copy the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/212462c38c02dfbd389e498c4833b33f/href">https://medium.com/media/212462c38c02dfbd389e498c4833b33f/href</a></iframe><p>In the code snippet above, we did the following:</p><ol><li>Imported the Redis, Bluebird and node-env-file modules</li><li>Set the environment file</li><li>Promisify the Redis module</li><li>Created a function that returns the Redis client or an error</li><li>Exported the client</li></ol><p>Since the connection to the Redis server is asynchronous, there is a high chance that our Redis client might not have been created and returned before we need it, this can be disastrous! Therefore to prevent a problem, the client() function returns a promise that either resolves with the Redis client or rejects with an error.</p><p>The client will be used to publish messages, subscribe to channels and listen for messages and to store published messages so that they are available after the broadcast.</p><p>Awesome right?!</p><h3>Helper Functions</h3><p>Now, we are going to declare and export some functions that we will use later during this tutorial. The reason why we are creating this helper functions, is to make our code base more readable and well organised. Our helper functions are going to help communicate with Redis to perform various actions which include</p><ul><li>Fetching all chat messages</li><li>Storing messages using list data type</li><li>Fetching all users</li><li>Adding users using set data type</li><li>Deleting a user</li></ul><p>Create a file functions.js in lib folder i.e lib/functions.js and add the code snippet below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c28b13a56c3978fc773879decc75b779/href">https://medium.com/media/c28b13a56c3978fc773879decc75b779/href</a></iframe><p>In the code snippet above, we imported the Redis client function and declared five functions: fetchMessages(), addMessage(), fetchActiveUsers(), addActiveUser() and removeActiveUser().</p><p>Before we continue, you might want to catch up on Redis data types <a href="https://redis.io/topics/data-types">here</a>. Let’s quickly take a look at what is happening in each function</p><ul><li>fetchMessages(): This function returns a promise that resolves with an array of messages or rejects with an error. Once client() promise has been resolved, it fetches all the messages in the list messages using the promisified <strong>LRANGE</strong> command lrangeAsync()</li><li>addMessage(): This function returns a promise that resolves with the length of the list added or rejects with an error message. Once client() promise has been resolved, it makes use of a promisified transaction, which queues up the <strong>RPUSH</strong> command that inserts message into the list messages and execute.</li><li>fetchActiveUsers(): This function returns a promise that resolves with an array of users or rejects with an error. Once client() promise has been resolved, it uses the promisified <strong>SMEMBER</strong> command smemberAsync() to fetch all users in the set users</li><li>addActiveUser(): Once client() promise has been resolved, this function uses a promisified transaction which queues up the <strong>SADD</strong> command that inserts a user into the set users and execute. It returns a promise that resolves with the number of element added to the set or rejects with an error message. A Redis set doesn&#39;t allow repetition of elements, therefore it makes it more convenient to store unique elements like usernames.</li><li>removeActiveUser(): Once client() promise has been resolved, this function uses a promisified transaction that queues up the <strong>SREM</strong> command that removes a user from the set users and then execute. This function returns a promise that resolves with the number of elements removed or rejects with an error message</li></ul><h3>Setting up the routes</h3><p>Routes basically handle how our web app send HTTP responses to various endpoints . A route usually has a path (e.g ‘/messages’), a verb (e.g GET) and a handler function. For this tutorial, we are going to need routes for the following</p><ul><li>The homepage of the app</li><li>The chat room</li><li>Fetching all the messages</li><li>Sending a message</li><li>Adding a user to the chat room</li><li>Removing a user from the chat room</li></ul><p>Create a file routes.js in the server folder created earlier , i.e server/routes.js and add the code below into it</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cb77e807bf456d2597a1a8cd83a4117/href">https://medium.com/media/4cb77e807bf456d2597a1a8cd83a4117/href</a></iframe><p>In the code snippet above, we did the following</p><ol><li>Imported Express, the Redis client and our helper functions</li><li>Created an instance of Express Router</li><li>Created two functions that return messages and active users stored on Redis, using the fetchMessages() and fetchActiveUsers() helper functions</li></ol><p>Next, let’s create some of our app routes, paste the code snippet below into routes.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae0bfb5296fb02fe73b26e7c049e5e5c/href">https://medium.com/media/ae0bfb5296fb02fe73b26e7c049e5e5c/href</a></iframe><p>In the code snippet above, we created and exported the routes for the following</p><ul><li>Displaying the homepage</li><li>Displaying the chat room</li><li>Returning all messages to the client</li><li>Returning all active users to the client</li></ul><p>Let’s create more routes that will be needed in the app, paste the code snippet below to routes.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a1a1a1555b6744b3a5df87af5cee929/href">https://medium.com/media/7a1a1a1555b6744b3a5df87af5cee929/href</a></iframe><p>In the code snippet above, we created and exported the routes for the following</p><ul><li>Creating a user</li><li>Deleting a user</li><li>Creating a message</li></ul><p>Let’s take a look at each of the routes above.</p><ul><li>createUser: This route handles adding a user to active users. Once a user has been added, a message announcing the new user to chat room and the updated active users are published using the Redis client. After that, the new message is then stored in Redis and a HTTP response is sent.</li><li>deleteUser: This route handles removing a user from active users. After the user is removed from the set users on Redis, a message announcing that the user has left the chat room and the the updated active users are published using the Redis client. Finally, the message is then stored on Redis and a HTTP response is sent.</li><li>createMessage: This route deals with messages sent by users in the chat room. Once a message is sent, it is published using the Redis client and then stored on Redis.</li></ul><h3>Setting up the server</h3><p>For this tutorial, we will use Express as our Node framework. Express is a minimal and flexible framework that provides a robust set of features for web and mobile applications. Create a folder server and create a file server.js in it, i.e server/server.js and copy the code snippet below into it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/456ed64cff880bcbab9e7210e7174dc2/href">https://medium.com/media/456ed64cff880bcbab9e7210e7174dc2/href</a></iframe><p>In the code snippet above, we did the following</p><ol><li>Imported Express, bodyParser, socket.io, path, node-env-file modules</li><li>Imported the Redis client we created in lib/redis.js</li><li>Imported the routes in server/routes.js</li><li>Set the environment file</li><li>Created the instance of Express</li><li>Set Pug as our view template engine and configured the path to our templates</li><li>Configured the path of our static files (stylesheets, scripts, etc)</li><li>Configured a middleware to use body-parser to parse the request body</li></ol><p>Next, we are going to subscribe to channels using the Redis client and listen for messages published on those channels. Once someone sends a message or joins the chat room, we will send the message or updated users list to all users using Socket.io. Copy the code snippet below to server.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/997268a82746239f25b4abc4a1c324dc/href">https://medium.com/media/997268a82746239f25b4abc4a1c324dc/href</a></iframe><p>We subscribed to two channels: chatMessages and activeUsers. Whenever a message is published, an event message is triggered. In the code snippet above, we did the following</p><ol><li>We declared the application endpoints</li><li>We started the server</li><li>We created an instance of Socket.io using the server we started</li><li>Once Socket.io establishes connection, we used the Redis client to subscribe to chatMessages and activeUsers channels</li><li>We checked for any message event triggered by a published message.</li><li>We then broadcasted the message with Socket.io, using message event for messages from chatMessage channel and users event for messages from activeUsers channel</li></ol><p>The / endpoint displays the homepage of the app, by rendering the index view template, while the /chat/:username renders the chat room using the room view template. We will cover the view templates later in this tutorial.</p><p>The GET /messages endpoint returns the history of messages shared in the chat room while GET /users returns the list of active users stored.</p><p>The POST /user endpoint handles adding users to active users. The POST /message handles publishing and storing of messages sent by users. The DELETE /user endpoint removes users from active users.</p><h3>The Frontend</h3><p>Remember we said something about Pug (formerly known as Jade) earlier on? Yes! We are using it as our view template engine, alongside CSS &amp; JQuery to build the front end client for the chat room.</p><p>Create a folder public/views views and a file in it master.pug master.pug so that the path is public/views/master.pug. This file will be the parent template, which other child templates will inherit. Copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fd060fa6f4e118f7378c6d783450436/href">https://medium.com/media/8fd060fa6f4e118f7378c6d783450436/href</a></iframe><p>When we extend this file, we can override specific areas labeled blocks. In the code snippet above, we have two blocks: content and script script. We will be extending the file in our child templates using those blocks. Also, Pug relies on indentation to create parent and child elements; therefore, we have to pay attention to that. You can check out pug’s documentation.</p><p>Next, we create index.pug, the template file we called in our GET / endpoint. It will hold the structure of the homepage. Copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77be1e1a310b6ca5cc474d45d4613002/href">https://medium.com/media/77be1e1a310b6ca5cc474d45d4613002/href</a></iframe><p>In the code snippet above, we are extending master.pug and overriding the block content to hold the structure we want. For the homepage, we are going to display a form to collect the username and submit it.</p><p>Next, let’s create the template file for the chat room, create file room.pug and paste the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f16c6db28fff618e2befb9612351596/href">https://medium.com/media/1f16c6db28fff618e2befb9612351596/href</a></iframe><p>Here, we added the socket.io script and the script handling the room, by overriding the block script.</p><p>Next, we need to style the structure we have defined above. Create a stylesheet style.css in public/assets/css, so that the path is public/assets/css/style.css and paste below into it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cb866e91d72c337ab5f1bd4564c78a1/href">https://medium.com/media/4cb866e91d72c337ab5f1bd4564c78a1/href</a></iframe><p>Finally, we need make our username registration form and chat room work. We are going to make this possible by using jQuery; a javascript library, to make requests to the endpoints.</p><p>First, let’s create the script to handle username registration. Create a folder js in public/assets and create file script.js in it, i.e public/assets/js/script.js and paste the code snippet below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72d3e252704b741fe41ef7838854211e/href">https://medium.com/media/72d3e252704b741fe41ef7838854211e/href</a></iframe><p>Once the user submits the form, a POST request is sent to /user with the username. If the response status is 200, we redirect to the chat room, else, we display a message that disappears after 2 seconds.</p><p>Next, let’s power our chat room. We need to display the chat history, active users, update the message wall when another user sends a message and also update the users list when a user joins or leave. Create a file chat.js in public/assets/js folder and copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2538ec9025ab42a75847a5913de4f005/href">https://medium.com/media/2538ec9025ab42a75847a5913de4f005/href</a></iframe><p>In the code snippet above, we did the following</p><ol><li>Created an instance of socket.io named socket</li><li>We fetched all the messages from the server and displayed it</li><li>We fetched the list of all active users from the server and displayed it</li><li>Send messages submitted by users to the server, either by clicking the send button or hitting enter.</li><li>Remove a user from the active user list on the server, just before the browser tab or window is closed</li><li>listen for messages emitted by the server and add them to the message wall using socket.io</li><li>Listen for updated active users list and display them using socket.io</li></ol><p>Our chat room is now ready for deployment!</p><h3>Deployment</h3><p>Before we deploy the application for usage, we need to quickly edit package.json in the root folder, so that we can start the app automatically, when we deploy. Add this to the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/341cfc32e35261aa0c534f00e3fb66e2/href">https://medium.com/media/341cfc32e35261aa0c534f00e3fb66e2/href</a></iframe><p>Finally, time to deploy! We will use a simple tool called now by Zeit. Let’s quickly install this, run this in your terminal:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7744f9da806f8a616d0eb3e0b32c7a89/href">https://medium.com/media/7744f9da806f8a616d0eb3e0b32c7a89/href</a></iframe><p>Once the installation is done, navigate to the project root folder in your terminal and run the now command. If it is your first time, you will be prompted to create an account. Once you are done, run the now command again, a URL will be generated and your project files uploaded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v94UjfunDp7We3aMWx0UWA.png" /></figure><p>You can now access the simple application via the URL generated. Pretty straightforward!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KngYTezWbJOQZAGsi1xTKA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CxXaYZ3lrpofWsTFWtvkrg.png" /></figure><h4>Conclusion</h4><p>We have been able to focus more on building our chat room, without having to worry about infrastructure. The ability to provision resources very fast, all from a single dashboard means we get to focus more on writing code. Don’t stop here, get creative and improve on the code in this tutorial, you can find the source code for this tutorial on <a href="https://github.com/iamfunsho/Chat-Room">Github</a>, fork it and do awesome stuffs.</p><h4>About Manifold</h4><p>Manifold is the first independent marketplace for developer services and the easiest way to manage and share everything from <a href="https://www.jawsdb.com/">JawsDB</a>, a simple postgres database to <a href="https://www.manifold.co/services/mailgun">Mailgun</a>, an API that makes sending and receiving email dead simple.</p><p>For more info visit <a href="https://www.manifold.co/">Manifold.co</a> or follow us on <a href="https://twitter.com/manifoldco">twitter</a>. This post is part of the <a href="https://www.manifold.co/compose">Manifold Content</a> program. Want to write for us?</p><figure><a href="https://www.manifold.co/stratusupdate/issue/latest"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ykcw1iepXAGjSVSXQDPsdg.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e8e5a578675" width="1" height="1" alt=""><hr><p><a href="https://medium.com/manifoldco/building-a-chat-room-in-30-minutes-using-redis-socket-io-and-express-9e8e5a578675">Building a chat room in 30 minutes using Redis, Socket.io and Express</a> was originally published in <a href="https://medium.com/manifoldco">manifoldco</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leveraging the Power of Elasticsearch: Autocomplete and Fuzzy Search]]></title>
            <link>https://medium.com/manifoldco/leveraging-the-power-of-elasticsearch-autocomplete-and-fuzzy-search-1d491d3e0b38?source=rss-bacef8d8fdc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d491d3e0b38</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Funsho]]></dc:creator>
            <pubDate>Wed, 20 Sep 2017 12:53:03 GMT</pubDate>
            <atom:updated>2017-09-28T16:04:10.472Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3eB_TOinSdjyy6rZOa9bEQ.png" /></figure><p>I’m going to show you how to build a simple search with autocomplete and fuzziness using <a href="https://www.manifold.co/">Manifold</a>, Express and deploying to Zeit. We will be building a simple web page that allows users to search for football players and display their details.</p><p>This tutorial will showcase some of the awesome features of Elasticsearch, but before we continue, we need to understand what autocomplete, fuzziness and Elasticsearch is.</p><h4>Introduction</h4><p>Autocomplete means giving users the option of completing words or forms by a shorthand method on the basis of what has been typed before. Pretty straightforward!</p><p>Fuzziness in the context of this article, means finding words that are resembling or similar by editing a character or more. There are four ways of finding fuzzily similar words: substitution, insertion, transposition and deletion. For example:</p><ul><li>Substitute r for n: clea_r_ → clea_n_</li><li>Insert k after c: tic → tic_k_</li><li>Transpose a and e: b_ae_ver → b_ea_ver</li><li>Delete r: po_r_t → pot</li></ul><p>Elasticsearch is an open-source, broadly-distributable, readily-scalable, really fast RESTful search and analytics engine. Accessible through an extensive and elaborate API, Elasticsearch can power extremely fast searches that support your data discovery applications.</p><p>If you don’t have any idea about Elasticsearch or Express, you should be able to follow the tutorial, however, I will recommend catching up:</p><ul><li><a href="https://expressjs.com/en/starter/installing.html">Express.js documentation</a></li><li><a href="https://www.elastic.co/guide/index.html">Elasticsearch documentation</a></li></ul><p>We are good to go!</p><h4>Set up</h4><p>We will use Manifold to create our elasticsearch instance and logging. If you don’t have a Manifold account, <a href="https://dashboard.manifold.co/register">head over and create one</a>, it is very easy to set up. Once you’ve logged in, the first thing we are going to do is create a Project for our app. To do that, click on the big grey block that has the plus sign, then name our project and give it a description (description is optional).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zFMFTVra3sAtR9orrg57eA.png" /></figure><p>You will then be able to create resources by clicking on Add a Resource button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cr1_lKu26TFi2aeSSNXGYA.png" /></figure><p>For this tutorial, we will using Bonsai Elasticsearch and LogDNA, so go ahead and provision both services. This will create a new Bonsai Elasticsearch and LogDNA accounts straight from your Manifold account. Awesome! To get your credentials, copy them by clicking on Copy to clipboard. You can also show your credentials by clicking Show credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bu1jZKmVI_56RdHXI2Sjzw.png" /></figure><p>For Bonsai Elasticsearch URL, you can get that from your Bonsai dashboard. To get to the dashboard, Click on the name of the Bonsai Elasticsearch resource created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NGz9599RXgjUMIgp4LjZsQ.png" /></figure><p>Create a .env file in your project root folder and paste our credentials for both Elasticsearch and LogDNA.</p><pre>BONSAI_URL=URL FROM BONSAI DASHBOARD <br>LOGDNA_KEY=LOGDNA KEY GENERATED</pre><h4>Installing Node modules</h4><p>Before we proceed, ensure you have node and npm installed. Open your terminal and run npm init, this will create a package.json file in your project root folder . Next, run this:</p><pre>npm install --save elasticsearch express logdna node-env-file pug nodemon</pre><p>We are installing the packages for Express framework, Elasticsearch and LogDNA. Pug will be used for our view template engine, node-env-file to for our .env file and nodemon to auto restart our server after making changes.</p><h4>Setting up</h4><p>LogDNA Logging makes it easy to find where and when things went wrong in our application. This is where LogDNA comes in. Create this file in the project root folder logdna.js and insert the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aab1b1408dff03493ef039d02cfa588/href">https://medium.com/media/7aab1b1408dff03493ef039d02cfa588/href</a></iframe><p>In the code snippet above, we required logdna and node-env-file modules installed earlier. Next, we set our environment file, with the help of node-env-file and created our instance of LogDNA with our arguments in logArgs. Finally, we exported our LogDNA instance.</p><h4>Basic concepts in Elasticsearch</h4><p>Before we continue, let’s quickly go through some basic concepts in Elasticsearch.</p><p>A cluster is a collection of one or more nodes (servers) that together hold your entire dataset and provides federated indexing and search capabilities across all nodes.</p><p>A node is a single server that is part of your cluster, stores your data, and participates in the cluster’s indexing and search capabilities.</p><p>An index is a collection of documents that have somewhat similar characteristics. For example, you can have an index for customer data, another index for a product catalog, and yet another index for order data.</p><p>A type is a logical category/partition of your index whose semantics is completely up to you. In general, a type is defined for documents that have a set of common fields.</p><p>A document is a basic unit of information that can be indexed. For example, you can have a document for a single customer, another document for a single product, and yet another for a single order.</p><p>Query DSL is a JSON-style domain-specific language that you can use to execute queries, provided by Elasticsearch. This is an example:</p><pre>GET /bank/_search<br>{<br>  &quot;query&quot;: { &quot;match_all&quot;: {} }<br>}</pre><h4>Setting up Elasticsearch</h4><p>For this tutorial, I created a json file with details of 2500 dummy players, download it <a href="https://file-duttpapgoc.now.sh/players.json">here</a>, create a folder data in the project root folder and paste the file in there, i.e data\player.json.</p><p>Now, create elasticsearch.js in the project root folder, then copy the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72d50e080d7e9e995ca44a6e1085ff1c/href">https://medium.com/media/72d50e080d7e9e995ca44a6e1085ff1c/href</a></iframe><p>In the code snippet above, we required node-env-file and added our .env file, fs to access the file system and elasticsearch. Next, we created an instance of Elasticsearch using the Bonsai Elasticsearch URL from Manifold, as host. Then we defined INDEX_NAME and INDEX_TYPE, that will used later.</p><p>Next, add the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3024d3d1029a381097f2354c59d05dbd/href">https://medium.com/media/3024d3d1029a381097f2354c59d05dbd/href</a></iframe><p>This function allows us to modify how we require the dummy data. This is necessary because our JSON file containing the dummy data, is not a valid JSON file and will fail when it is parsed.</p><p>Next, let’s add the functions that checks if the index exists, creates the index and deletes the index. Copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9cd3164ab4fbef9ae0b175ac5bd151e/href">https://medium.com/media/e9cd3164ab4fbef9ae0b175ac5bd151e/href</a></iframe><p>The next step is to create the index mapping. Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. Mapping is used to define if a field can be used for suggestions or is a date, string, number etc. Add the code snippet below to the existing elasticsearch.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9808f6301c42195cc65dceab8743728/href">https://medium.com/media/d9808f6301c42195cc65dceab8743728/href</a></iframe><p>In our mapping function above, we defined both firstName and lastName as type completion, this type allows completion suggestions to be made using those two fields.</p><p>For the search to work, we need to add documents. Therefore, we need a function to bulk add our dummy data. Add the code snippet below to elasticsearch.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbd496418492487d9433b085bb61084f/href">https://medium.com/media/bbd496418492487d9433b085bb61084f/href</a></iframe><p>From the code above, readDataFile() fetches our dummy data from the JSON file, which becomes the body of the request.</p><p>Next, we want Elasticsearch to be able to suggest players based on their first name and last name. However, names of player are sometimes difficult to spell, therefore, we want to be able to suggest players that have their first name or lastname fuzzily similar to the user’s search entry. Add the code below to the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ae9888c381a66c5202e7521f3314956/href">https://medium.com/media/2ae9888c381a66c5202e7521f3314956/href</a></iframe><p>From the code above, we have two suggesters: firstNameSuggester and lastNameSuggester. The first will return first names matching the search word while the second suggester will return for last names.</p><p>We want to be able to fetch the details of a particular player when we search, therefore, we need to have a search function. Place the code snippet below in your code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba98077922fba3d518c1f24ad76c1fa0/href">https://medium.com/media/ba98077922fba3d518c1f24ad76c1fa0/href</a></iframe><p>The function above, searches and returns the details of a player, using the id passed.</p><p>Finally, we need to export the functions, so that they can be used in other files. Paste the code below at the end of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8f512ae4cf54c69a3d98de39c5656a3/href">https://medium.com/media/d8f512ae4cf54c69a3d98de39c5656a3/href</a></iframe><h4>Defining app routes</h4><p>Routes are application endpoints that determine how responses are sent to a client’s request. In this application, we will need three routes: / to return the homepage, /suggest/:text/:size to return suggestions and /stat/:id to return the details of a player.</p><p>Create a folder routes, create a file index.js and paste the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65d8bcc4900d546315b70c12a3fcce3f/href">https://medium.com/media/65d8bcc4900d546315b70c12a3fcce3f/href</a></iframe><p>In the code above, we created a router as a module. This router renders the homepage and sends the title of the page to the page template named master. The page template will be discussed later.</p><p>Next, create a new file suggest.js in the routes folder and add the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a717a6973259bad4000307a1f3fbc34/href">https://medium.com/media/1a717a6973259bad4000307a1f3fbc34/href</a></iframe><p>The router module above requires elasticsearch.js and calls the function getSuggestions, which returns a client-specified amount of suggestions for the search word from the client. Also, we logged the action using LogDNA.</p><p>Finally for routes, create a file stats.js in the routes folder and copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c707c5e1ed7849ede76fc55b4557bf6/href">https://medium.com/media/8c707c5e1ed7849ede76fc55b4557bf6/href</a></iframe><p>This route returns the details of a player based on the id passed into the getStat function. Also, the action is logged using the instance of LogDNA exported as a module in logdna.js</p><h4>Setting up the server with Express</h4><p>For the application to work, we need to run it on a server, this is where Express comes in. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. In the project root folder, create app.js file and copy the code below into it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e43c7dbc416f7f35ee71d44ac1c97ef/href">https://medium.com/media/7e43c7dbc416f7f35ee71d44ac1c97ef/href</a></iframe><p>In the code snippet above, we required the express and path module, also, we required the LogDNA &amp; Elasticsearch modules and the routes we created earlier. Then, we created an instance of Express named app</p><p>Next, add the code snippet below to set the view engine, views folder and static files directory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87770b0f666651d5d2f8b3f1bf85b482/href">https://medium.com/media/87770b0f666651d5d2f8b3f1bf85b482/href</a></iframe><p>For this tutorial, we are using the Pug (formerly known as Jade), a high performance template engine.</p><p>Next, we need to check the setup of our Elasticsearch index and make sure that our data is stored correctly. Copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/013920050b57ad8571f58c11d594bbf4/href">https://medium.com/media/013920050b57ad8571f58c11d594bbf4/href</a></iframe><p>In the code snippet above, we did the following:</p><ol><li>Check if the index exists</li><li>Delete the index if it exists and log the outcome</li><li>Create the index and log the outcome</li><li>Update the index with mapping and log the outcome</li><li>Bulk insert the dummy documents and log the outcome</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8466a20a582823014a451e0915d8ae3/href">https://medium.com/media/d8466a20a582823014a451e0915d8ae3/href</a></iframe><p>Finally, we created our app endpoints and assigned the router modules accordingly. After that, we started the server on the port specified and logged it.</p><h4>Building the Frontend</h4><p>Now, we will build a simple frontend web page to consume the backend API, using Pug template engine to generate HTML and jQuery.</p><p>Create a folder views and create a file index.pug, the path should be views/index.pug and copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e5548da853103c0f68dac2fa42c9a85/href">https://medium.com/media/7e5548da853103c0f68dac2fa42c9a85/href</a></iframe><p>In the above code snippet, we created the HTML structure. Pug relies on indentation to create parent and child elements, therefore, we have to pay attention to that.</p><p>Next, we need to style the web page using CSS, create a stylesheet file public/css/style.css and paste the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a8f0673f4a1b58fe9cca5ca91f0125c/href">https://medium.com/media/5a8f0673f4a1b58fe9cca5ca91f0125c/href</a></iframe><p>The above style declarations will make the page look a bit nice and simple.</p><p>Finally, we need to be able to show suggestions as users search, click on a result and get the player’s details. In this tutorial, we are going to make that possible by using jQuery; a javascript library, however, if you want to use another framework or library, feel free.</p><p>Now, let’s create a new javascript file using the path public/js/script.js, then copy the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1e2b21b9a0f61cea377681449f6f2a6/href">https://medium.com/media/b1e2b21b9a0f61cea377681449f6f2a6/href</a></iframe><p>In the script above, we have two functions: suggestion and stat as well as five event listeners: change, paste, keyup, two clicks.</p><p>The event listener $(&#39;.searchbar&#39;).on(&quot;change paste keyup&quot;, function() {...}); checks for when a user changes the value of the search box and sends a request using the suggestion function, which returns suggestions.</p><p>The event listener $(document).on(&#39;click&#39;, &#39;.option&#39;, function(el){...}); is triggered when a user clicks on any of the suggested results, this sets the value of a hidden field with the ID of the player and also sets the value in the search box to the player’s name.</p><p>Finally, the event listener $(document).on(&#39;click&#39;, &#39;.submit&#39;, function(el){...} is triggered when the user clicks on the Get Details button. This fetches the ID stored in the hidden input field, sends a request to Elasticsearch using the stat function and displays the player’s details below the search box.</p><p>The app is ready!</p><h4>Deployment</h4><p>Before we deploy, let’s quickly edit package.json in the root folder, so that we can start the app automatically, when we deploy. Add this to the file:</p><pre>&quot;scripts&quot; : {<br> &quot;start&quot;: &quot;nodemon ./app.js&quot;<br>}</pre><p>Finally, time to deploy! We will use a simple tool called now by Zeit. Let’s quickly install this, run this in your terminal:</p><pre>npm install -g now</pre><p>Once the installation is done, navigate to the project root folder in your terminal and run the now command. If it is your first time, you will be prompted to create an account. Once you are done, run the now command again, a URL will be generated and your project files uploaded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hWYYErWDW9cfrAk58-82gQ.png" /></figure><p>You can now access the simple application via the URL generated. Pretty simple!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-APFYHeZWDC0LiwIhOEqhA.png" /></figure><p>Also, we can view logs on LogDNA dashboard to make sure everything is working as it should be. To access the dashboard, see the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iHmondA-O9ep933VWOo7Ng.png" /></figure><p>The dashboard should look like below, with the log from our application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPmCRARV_rS14UuK0rtHjg.png" /></figure><h4>Conclusion</h4><p>We have been able to focus more on building our simple app, without worrying about infrastructure. The ability to provision resources very fast, all from a single dashboard means we get to focus more on writing code. Don’t stop here, get creative and improve on the code in this tutorial, you can find the source code for this tutorial on <a href="https://github.com/iamfunsho/Elasticsearch-Tutorial">Github</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d491d3e0b38" width="1" height="1" alt=""><hr><p><a href="https://medium.com/manifoldco/leveraging-the-power-of-elasticsearch-autocomplete-and-fuzzy-search-1d491d3e0b38">Leveraging the Power of Elasticsearch: Autocomplete and Fuzzy Search</a> was originally published in <a href="https://medium.com/manifoldco">manifoldco</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>