<?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 Rene Loperena on Medium]]></title>
        <description><![CDATA[Stories by Rene Loperena on Medium]]></description>
        <link>https://medium.com/@rloperena?source=rss-4f0df1deeedb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6VE4Qo2dmP4CTdNW5Qn7GA.jpeg</url>
            <title>Stories by Rene Loperena on Medium</title>
            <link>https://medium.com/@rloperena?source=rss-4f0df1deeedb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 09:10:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rloperena/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[Quick guide to Socket.io basics]]></title>
            <link>https://medium.com/@rloperena/quick-guide-to-socket-io-basics-59b07c39c6ec?source=rss-4f0df1deeedb------2</link>
            <guid isPermaLink="false">https://medium.com/p/59b07c39c6ec</guid>
            <category><![CDATA[sockets]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Rene Loperena]]></dc:creator>
            <pubDate>Tue, 08 Sep 2015 02:04:11 GMT</pubDate>
            <atom:updated>2015-09-08T02:04:11.257Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of months ago I started learning Node.js and one of the first things I ran into was Socket.io; it seemed like a pretty exciting framework but I couldn’t find an easy to understand, basic guide to begin with. I am writing this guide so you don’t have to struggle with the Socket.io/Node documentation like I did, so let’s get started!</p><p>Socket.io is a JavaScript library that allows two-way communication between your Node.js server and your web client; this is possible using sockets, which is a connection between 2 computers over the network. Don’t worry, it is not as complicated as it sounds, Socket.io abstracts all the difficulty and makes it easy for us!</p><h3>The Hello Event!</h3><p>The first concept we need to understand when using Socket.io is that it is based on events, where:</p><ol><li>Our Server will <strong>emit</strong> an event.</li><li>Our Client will react <strong>on</strong> an event.</li></ol><h4>Server</h4><pre>socket.emit(&#39;hello&#39;);</pre><h4>Client</h4><pre>socket.on(&#39;hello&#39;, function(){  <br>   console.log(&#39;hello world!&#39;);<br>});</pre><p>When we emit the ‘hello’ event on the server, the socket object on the client will receive the event and it will react to it with the given callback, in this case, our anonymous callback will log <em>‘hello world!’</em>.</p><h3>Multiple events and handlers</h3><p>We can create as many events as we want and we are able to catch them and react to them in different ways.</p><h4>Server</h4><pre>socket.emit(&#39;goodbye&#39;);</pre><h4>Client</h4><pre>socket.on(&#39;hello&#39;, function(){  <br>   console.log(&#39;hello world!&#39;);<br>});<br>socket.on(&#39;goodbye&#39;, function(){  <br>   console.log(&#39;goodbye world!&#39;);<br>});</pre><p>In this case we are emitting a <em>‘goodbye’</em> event on our Server, thus the client will catch this event and print <em>‘goodbye world!’</em> on our console.</p><h3>Sending objects within our events</h3><p>Sometimes we not only want to fire an event, but we also want to send some information to the client. To do that we simply add the objects we want to send as parameters to the emit, and we receive them on our callback function.</p><h4>Server</h4><pre>var person = {name: &#39;Rene&#39;, age: 26};  <br>socket.emit(&#39;person&#39;, person);</pre><h4>Client</h4><pre>socket.on(&#39;person&#39;, function(person){  <br>   console.log(person.name, &#39;is&#39;, person.age, &#39;years old.&#39;);<br>});</pre><h3>Two-way Communication</h3><p>Remember when I said Socket.io allows Two-way communication? Well, this means that we can not only send information from the Server to the Client, but also vice-versa.</p><h4>Server</h4><pre>var person = {name: &#39;Rene&#39;, age: 26};  <br>socket.emit(&#39;person&#39;, person);</pre><pre>socket.on(&#39;confirmation&#39;,function(){  <br>   console.log(&#39;The client received the person&#39;);<br>});</pre><h4>Client</h4><pre>socket.on(&#39;person&#39;, function(person){  <br>   console.log(person.name, &#39;is&#39;, person.age, &#39;years old.&#39;);<br>   socket.emit(&#39;confirmation&#39;);<br>});</pre><p>In this case, after the person is received by the client, we will send a <em>‘confirmation’ </em>event inside the callback to let the server know that the client received the person. (Yes, we can emit events inside the callbacks for other events).</p><h3>Creating the Socket.io connection</h3><h3>Server Side</h3><p>Now that we understand how to emit events, send data within them and react to them, it’s time to create the socket between the client and the server.</p><p>On our server side (Node.js) we will need to install Socket.io using the node package manager (NPM). To do this we can simply go to the project directory and:</p><pre>npm install socket.io</pre><p>Once we have it installed, we can require it on our application.</p><h4>Server</h4><pre>var app = require(&#39;http&#39;).createServer(handler)  <br>var io = require(&#39;socket.io&#39;)(app);</pre><p>Once we create our Socket.io object and attach it to our http server we need to handle client connections. On a client connection the server will emit the ‘<em>connection</em>’ event, this connection event can be handled by the <em>io</em> object in order to create a<em> socket</em>.</p><h4>Server</h4><pre>var app = require(&#39;http&#39;).createServer();  <br>var io = require(&#39;socket.io&#39;)(app);<br>app.listen(80);</pre><pre>io.on(&#39;connection&#39;, function (socket) {<br>  // (Insert socket code here)<br>});</pre><p>Each socket object represents a communication <em>tunnel</em> between the client and the server, and a new one will be created each time a client connects to the server.</p><h3>Client Side</h3><p>On the client side we need to reference the Socket.io client JavaScript file.</p><pre>&lt;script src=&quot;https://cdn.socket.io/socket.io-1.3.5.js&quot;&gt;&lt;/script&gt;</pre><p>Once we do that, we can simply create a new socket by connecting to our own http server using the <em>io</em> object (client).</p><pre>&lt;script&gt;<br>  var socket = io(&#39;http://localhost&#39;);<br>  // (Insert socket code here)<br>&lt;/script&gt;</pre><h3>Connecting the dots</h3><p>Now that we have a socket on both the server and the client, we can start emitting events like we did before.</p><h4>Server</h4><pre>var app = require(&#39;http&#39;).createServer();<br>var io = require(&#39;socket.io&#39;)(app);<br>app.listen(80);</pre><pre>io.on(&#39;connection&#39;, function (socket) {<br>  socket.on(&#39;message&#39;,function(message){<br>    console.log(message); <br>  });<br>  socket.emit(&#39;message&#39;,&#39;Hello, my name is Server&#39;);<br>});</pre><h4>Client</h4><pre>&lt;script&gt;<br>  var socket = io(&#39;http://localhost&#39;);<br>  socket.on(&#39;message&#39;,function(message){<br>    console.log(message); <br>  });<br>  socket.emit(&#39;message&#39;,&#39;Hello, my name is Client&#39;);<br>&lt;/script&gt;</pre><p>Voila! we have successfully created a Socket.io connection. Hopefully you have a much better understanding of the Socket.io basics and I didn’t confuse you that much!</p><p>Thanks for reading and I would appreciate if you take some time to give me some feedback and help me improve this guide! Either way, have fun with Socket.io :)</p><p>Here’s a much more friendly version of the code if you want to run it on your own:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d443d8e5080244662bcd6259edec099/href">https://medium.com/media/6d443d8e5080244662bcd6259edec099/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59b07c39c6ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>