title: Sails.js - How to setup the sails socket config?
body: <p>I have studied sockets, socket.io and sails yesterday and I have to say that I'm pretty lost. I've never used them before. I thought I understand but I'm not sure anymore.</p>
  
  <p>I have based my work on <a href="http://socket.io/get-started/chat/" rel="nofollow">http://socket.io/get-started/chat/</a>. I'm working with the framework Sails.js, which add its own method to deal with sockets and so on.</p>
  
  <p>In <code>config/sockets.js</code> we have a <code>onConnect</code> and <code>onDisconnect</code> methods. See <a href="https://github.com/balderdashy/sails-docs/blob/master/reference/sails.config/sails.config.sockets.md" rel="nofollow">https://github.com/balderdashy/sails-docs/blob/master/reference/sails.config/sails.config.sockets.md</a> and (source code) <a href="https://gist.github.com/Vadorequest/568afc14294f1448ab55" rel="nofollow">https://gist.github.com/Vadorequest/568afc14294f1448ab55</a></p>
  
  <p>I will compare sails and socket.io, let's take this code:</p>
  
  <pre><code>io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
      console.log('user disconnected');
    });
  });
  </code></pre>
  
  <p>When the socket.io is open (connection) it defines <code>listeners</code> like <code>socket.on('disconnect')</code> which are waiting for an emmited request, from the client or the server using <code>socket.emit</code> method.</p>
  
  <p>What I thought is that the sails <code>onConnect</code> is equivalent to the socket.io <code>io.on(connection)</code>. Then the sails <code>onDisconnect</code> method is equivalent to <code>socket.on('disconnect')</code>, inside the <code>io.on(connection)</code>, it would be like a shortcut, sails proper way to handle this specific event and respect its standards (method starting by <code>on</code>).</p>
  
  <p>But I've shown my code to a friend who has already worked with socket.io and sails and he tells me I'm wrong and what I'm doing is defining a listener inside another listener which would result in a messy mess (at least).</p>
  
  <p>I would like to know if I misunderstood how to setup sails sockets.</p>
  
  <p>If I'm wrong I'll cry but I'll get over it.</p>