<?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 Cristian Botella Hurtado on Medium]]></title>
        <description><![CDATA[Stories by Cristian Botella Hurtado on Medium]]></description>
        <link>https://medium.com/@cri.bh6?source=rss-4050f58253af------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*mU2XIvFity7tmZOEBizKzQ.png</url>
            <title>Stories by Cristian Botella Hurtado on Medium</title>
            <link>https://medium.com/@cri.bh6?source=rss-4050f58253af------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 02:41:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cri.bh6/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[Write a very simple IoT API using ExpressJS and MQTT]]></title>
            <link>https://medium.com/@cri.bh6/in-this-simple-example-im-going-to-show-how-to-write-a-very-simple-expressjs-api-that-uses-mqtt-to-57aa3ecdcd9e?source=rss-4050f58253af------2</link>
            <guid isPermaLink="false">https://medium.com/p/57aa3ecdcd9e</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[mqtt]]></category>
            <category><![CDATA[iot]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[express]]></category>
            <dc:creator><![CDATA[Cristian Botella Hurtado]]></dc:creator>
            <pubDate>Fri, 27 Apr 2018 11:05:41 GMT</pubDate>
            <atom:updated>2018-05-21T11:09:04.565Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cg6KhC4_kYqbHfU9cFMKtg.jpeg" /></figure><p>In this simple example I’m going to show how to write a very simple ExpressJS API that uses MQTT to send/receive messages. This can be useful to communicate the API with IoT devices.</p><p>A brief explanation of what is MQTT (probably you already know)</p><p>Extracted from mqtt.org FAQ</p><p><a href="http://mqtt.org/faq">http://mqtt.org/faq</a></p><blockquote>MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.</blockquote><p>check out the code in this github repo <a href="https://github.com/cbh6/express-api-mqtt-example">https://github.com/cbh6/express-api-mqtt-example</a></p><h3>Writing the mqtt handler</h3><p>First of all we need to install the javascript mqtt client library. <a href="https://www.npmjs.com/package/mqtt">https://www.npmjs.com/package/mqtt</a></p><pre>npm install mqtt --save</pre><p>And create a file called <strong>mqtt_handler.js</strong></p><p>mqtt.js has a really simple example explaining how to connect, publish and subscribe</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb79428b46092ce40aa18406290873f7/href">https://medium.com/media/cb79428b46092ce40aa18406290873f7/href</a></iframe><p>We are going to do almost the same in our simple MqttHandler class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bda14c098a8b172238567b915617d8c9/href">https://medium.com/media/bda14c098a8b172238567b915617d8c9/href</a></iframe><p>As you can see we have got a connect() method where we create a connection.</p><p>After that we are subscribing to `mytopic` and printing any message we receive.</p><p>We also have a method to publish a message to the topic `mytopic`, this method is going to be called from our API.</p><h3>Building the API</h3><p>This is very simple</p><p>First lets install express and body parser</p><pre>npm install express --save</pre><pre>npm install body-parser --save</pre><p>body-parser Parses incoming request bodies</p><p>Now we need to create our API entry point <strong>app</strong>.<strong>js</strong> with a very simple code to start a server and listen to <strong>/send-mqtt</strong> [post] requests. When someone hit the endpoint with some message in the request body, we are going to send the message using our MqttHandler</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60e4939570458ed4fd8a12616974510a/href">https://medium.com/media/60e4939570458ed4fd8a12616974510a/href</a></iframe><h3>Testing it</h3><p>Now we can go ahead and run our API by typing node app.js in the terminal, you should get something like this printed on your terminal</p><pre>app running on port. 3000</pre><pre>mqtt client connected</pre><p>Lets make a post request to <a href="https://localhost:3000/send-mqtt">http://localhost:3000/send-mqtt</a> using postman.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7N86nnGzhSyViEBL0_7f5g.png" /></figure><p>As you can see in your terminal. Our server received the mqtt message and printed it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*SrFhGZQe5Oyxje4Nz8cQAA.png" /></figure><p>As you can see, building an ExpressJS API that uses mqtt protocol to send/receive messages is very simple.</p><p>That can be quite useful for developing IoT systems, where we often need to wait for the device to respond to check if the task was completed etc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57aa3ecdcd9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Node.js chat app with Vue.js and Socket.io]]></title>
            <link>https://medium.com/@cri.bh6/building-a-real-time-chat-with-vuejs-nodejs-and-socketio-18f66e8da5d7?source=rss-4050f58253af------2</link>
            <guid isPermaLink="false">https://medium.com/p/18f66e8da5d7</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[chat]]></category>
            <category><![CDATA[socketio]]></category>
            <dc:creator><![CDATA[Cristian Botella Hurtado]]></dc:creator>
            <pubDate>Mon, 30 Oct 2017 08:03:51 GMT</pubDate>
            <atom:updated>2018-07-30T07:39:07.181Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xx7Ae6QSUV47j8DmrLCPgg.png" /></figure><p>As part of my learning I decided to make some application and found interesting the possibility of developing a chat with NodeJS, SocketIO and VueJS.</p><p>Let’s go!</p><p>Our chat:</p><p>As any other chat application, there will be an area where we are going to show all the messages and an input to send new messages to the people. Also, we will have a list with all connected users.</p><p>The first time a client gets into the chat we will ask for username before start chatting.</p><p>Our final app will look like this:</p><h4>You can check out the code on <a href="https://github.com/cbh6/vuejs-socketio-chat">github</a></h4><h4>Or watch it running on this <a href="https://funnymsg-vue.herokuapp.com/">live demo</a></h4><h3>Setup environment</h3><p>We assume that we have NodeJS installed on our machine, otherwise we should go to the <a href="https://nodejs.org/es/">official site</a> and download it.</p><p>This is the folder structure of the project:</p><pre>|-- node_modules<br>|-- app<br>|   |-- stylesheets<br>|   |   |-- style.css<br>|   |-- images<br>|   |   |-- favicon.ico<br>|   |-- app.js <br>|   |-- index.html<br>|-- package.json<br>|-- server.js</pre><p>In an empty directory, create a package.json file with the following content:</p><pre>{<br>  &quot;name&quot;: &quot;vuejs-socketio-chat&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;Simple chat with VueJS, NodeJS and SocketIO&quot;,<br>  &quot;main&quot;: &quot;server.js&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;bulma&quot;: &quot;^0.5.1&quot;,<br>    &quot;dateformat&quot;: &quot;^2.0.0&quot;,<br>    &quot;express&quot;: &quot;^4.15.4&quot;,<br>    &quot;font-awesome&quot;: &quot;^4.7.0&quot;,<br>    &quot;serve-favicon&quot;: &quot;^2.4.3&quot;,<br>    &quot;socket.io&quot;: &quot;^2.0.3&quot;,<br>    &quot;vue&quot;: &quot;^2.4.2&quot;<br>  }<br>}</pre><p>Navigate to your folder and execute:</p><pre>npm install</pre><p>Within a few seconds, you’ll have all the necessary dependencies downloaded to the node_modules directory.</p><h3>Developing the Backend</h3><p>Let’s create a simple server, which will deliver the application’s HTML page. In next steps we will get deep into the real time communication.</p><p>Create server.js file with the following core expressjs code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56557b97d1efe7d38d1fdf46ca7b9923/href">https://medium.com/media/56557b97d1efe7d38d1fdf46ca7b9923/href</a></iframe><p>We are using some extra modules such as dateFormat and favicon (these 2 are not really necessary)</p><h3>What about the real time comunication?</h3><p>We will need both server and client listening and emitting events:</p><ul><li><strong>When user connects to the chat</strong> -&gt; Server emits 2 events to other clients: init-chat and update-users. The first one will update client message list with all server stored messages, the second one will do the same with users.</li><li><strong>When user sets the username</strong> emits an event called add-user -&gt; Server listen to this event, updates the user list and emits a new event which tells the rest of users to update their user list.</li><li>Same <strong>when the user sends a message</strong>, the client emits an event called send-message -&gt; Server updates message list and emits an event which tells the rest of users to update their message list</li><li><strong>When user disconnects</strong>: server emits an event for updating the user list of all connected clients.</li></ul><p>So, in server.js we are going to put this piece of code under previous step code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cf42ff00c9a5358cd8bd4f48b776b83/href">https://medium.com/media/0cf42ff00c9a5358cd8bd4f48b776b83/href</a></iframe><h3>What about the view ?</h3><p>After configuring our node http server with SocketIO we need the client ready to listen and emit those events.</p><p>As we said in the first part of the post there are some areas that can be separated in some components:</p><ul><li>Username input</li><li>Message box</li><li>Message input</li><li>Users list</li></ul><p>Message list could also be a component, but this will be omitted for simplification.</p><p>We are going to need two new files called index.html and app.js inside the app folder. The second one will contain all SocketIO and VueJS stuff.</p><p>Let’s create the index.html file. We are going to put some simple template.</p><p>Notice we are using bulma to style our app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8606543305868411c673dddf3abcd80a/href">https://medium.com/media/8606543305868411c673dddf3abcd80a/href</a></iframe><p>Next, create the app.js file and add the following code (self explained by comments):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a8c6fceec438ab213e9b947edccb84c/href">https://medium.com/media/1a8c6fceec438ab213e9b947edccb84c/href</a></iframe><p>We separate html and js code in two files for better readability but this is not necessary.</p><p>Inside the vue instance we will need to store some data:</p><ul><li>message list</li><li>user list</li><li>username</li><li>isLogged -&gt; this one will be true as soon as the user inputs his name. We need this to control the visibility of name input and chat box. (When the user loads the chat, the username is requested. After the username is set, the chat will be shown)</li></ul><p>We also implemented the socket events.</p><h3>And… where are components and chat box ?</h3><p>Our index.html finnally will look like this. (Code is commented for better explanation)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e40cf8d35d55b107eb7adfeb7ce2861c/href">https://medium.com/media/e40cf8d35d55b107eb7adfeb7ce2861c/href</a></iframe><p>And we will need to update our app.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2eb8eb4d33e758cb7c1f10035e5f4746/href">https://medium.com/media/2eb8eb4d33e758cb7c1f10035e5f4746/href</a></iframe><h3>Conclusion</h3><p>NodeJS is an extremely useful technology, and provides us with a great deal of power and joy, especially when considering the fact that we can write pure JavaScript. As you can see, with only a few lines of code, we managed to write a fully functional real time chat application.</p><p>VueJS allows us to display the data easily and separate the view in some little components.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18f66e8da5d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>