title: socket.io/?EIO=3&transport=polling&t=1345678 no charging
body: <p>I made a chat using node.js, express and socket.io. Here is the app.js code:</p>
  
  <pre><code>var express = require('express'),
      app = express(),
      server = require('http').createServer(app),
      io = require('socket.io').listen(server);
  
  server.listen(process.env.PORT || 8080);
  
  
  
  app.use(express.static(__dirname + '/'));
  
  
  io.on('connection', function(socket){
    socket.on('message sent', function(data){
      io.emit('receive', { msg: data.msg, uname: data.uname, uid: data.uid, uimg: data.uimg });
    });
  
    socket.on('message sent room', function(data){
      console.log(data.mid);
      io.in(data.uroom).emit('receive', { msg: data.msg, uname: data.uname, uid: data.uid, uimg: data.uimg, mid: data.mid });
    });
  
    socket.on('join', function(data){
      io.emit('join user', { uname: data.uname, uid: data.uid });
    });
  
    socket.on('create', function (room) {
      socket.join(room);
    });
  
  });
  </code></pre>
  
  <p>and here is my code on my haml file</p>
  
  <pre><code>  %script{:src =&gt; "https://cdn.socket.io/socket.io-1.2.0.js"}
  
      - if Rails.env.production?
        :javascript
          var socket = io.connect('https://myherokuappurl.herokuapp.com/');
      - else
        :javascript
          var socket = io.connect('http://localhost:8080/');
  
      :javascript
        var userId = "#{current_user.id}";
        var userName = "#{current_user.name}";
        var userImg = "#{@userImage}";
        var room  = "#{@conversation.id}";
        var messageId = 0;
  
        $(document).ready(function(){
          socket.emit('create', room);
          $( "#m" ).focus();
          var wtf    = $('.panel-chat');
          var height = wtf[0].scrollHeight;
          wtf.scrollTop(height);
        });
  
        $('form').submit(function(){
          var message = $('#m').val();
          $.ajax({
            type: "POST",
            url: "#{dashboard_messages_path}",
            dataType : 'json',
            data: {'message' : {'conversation_id': room,
                   'body': message,
                   'user_id': userId} },
            success: function(data) {
              console.log(data.id);
              messageId = data.id;
              socket.emit('message sent room', {msg: $('#m').val(), uname: userName, uid: userId, uimg: userImg, uroom: room, mid: messageId});
              $('#m').val('');
            }
          });
          return false;
        });
  
        socket.on('receive', function(data){
          var message = '&lt;tr&gt;&lt;td class="td-chat"&gt;&lt;div class="text-center"&gt;&lt;img alt="'+data.uname+'" class="avatar center-block" src="'+data.uimg+'" height="60" width="60"&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;small&gt;&lt;strong&gt;'+data.uname+':&lt;br&gt;&lt;/strong&gt;&lt;/small&gt;'+data.msg+'&lt;/td&gt;&lt;/tr&gt;';
          var currentMessageId = data.mid;
  
          if (data.uid != userId) {
            $.ajax({
                type: "GET",
                url: "#{dashboard_message_read_path}",
              dataType : 'json',
                data: {'message_id':currentMessageId }
              });
          }
          $("#table-chat").append(message);
          var wtf    = $('.panel-chat');
          var height = wtf[0].scrollHeight;
          wtf.scrollTop(height);
        });
  
        socket.on('join user', function(data){
          if (data.uid != userId) {
             $("#userjoin").text(data.uname + " se ha unido!").show().fadeOut(5000);
          }
        });
  </code></pre>
  
  <p>But in order to make this work I have to refresh the chat page. I notice that the first time the user enters to the chat page, it does not have the following file:</p>
  
  <blockquote>
    <p><a href="https://myherokuappurl.herokuapp.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0" rel="noreferrer">https://myherokuappurl.herokuapp.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0</a></p>
  </blockquote>
  
  <p>or this on my local machine</p>
  
  <blockquote>
    <p><a href="http://localhost:8080/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0" rel="noreferrer">http://localhost:8080/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0</a></p>
  </blockquote>
  
  <p>So the chat does no send the message, instead when you press enter to send the message, the page refreshes and this time I have the following file and everything works great.</p>
  
  <blockquote>
    <p><a href="https://myherokuappurl.herokuapp.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0" rel="noreferrer">https://myherokuappurl.herokuapp.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0</a></p>
  </blockquote>
  
  <p>or this on my local machine</p>
  
  <blockquote>
    <p><a href="http://localhost:8080/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0" rel="noreferrer">http://localhost:8080/socket.io/?EIO=3&amp;transport=polling&amp;t=1xxxxxx-0</a></p>
  </blockquote>
  
  <p>What could be the problem here, because this behavior occurs on my localmachine and production.</p>
  
  <p>Thanks in advance for your help.  </p>