1

enter image description here

This is my server.js code:

var express =require('express'),
http = require('http');
var app= express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

users = [];
connections = [];

server.listen(process.env.Port || 3000);
console.log('server running ...')

app.get('/', function(req, res) {
 res.sendFile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket){
 connections.push(socket);
 console.log('Connected: %s sockets connected', connections.length);

 socket.on('disconnect',function(data){
 users.splice(users.indexOf(socket.username),1);
 updateUsernames(); 
 connections.splice(connections.indexOf(socket),1);
 console.log('Disconnected: %s sockets conected', connections.length);
});
 //send message
 socket.on('send message', function(data){
  console.log(data)
  io.sockets.emit('new message',{msg: data, user: socket.username});
 });

 //new User

 socket.on('new user', function(data,callback){
  callback(true);
  socket.username = data;
  users.push(socket.username);
  updateUsernames();
 });

 function updateUsernames(){
  io.sockets.emit('get users', users)
 }
});

Here is my html file:

<!DOCTYPE html>
<html>
<head>
 <title>IO chat</title>
 <link rel = "stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 <script src="https://code.jquery.com/jquery-latest.min.js"></script>
  <script src="/socket.io/socket.io.js"></script>
 <style >
  body{
   margin-top: 30px;
  }
  #messageArea{
    display: none;
   }
 </style>
</head>
<body>
 <div class = "container">

 <div id = "userFormArea" class= "row">
  <div class = "col-md-12"> 
   <form id = "userForm"> 
     <div class = "form-group">
      <label>Enter Username</label>
      <input class = "form-control" id = "username"> 
      <br/>
      <input type="submit" class = "btn btn-primary" value = " Login"/>
     </div>
    </form>

  </div>
 </div>
  <div class = "row" id = "messageArea">
   <div class = "col-md-4">
    <div class = "well">
     <h3> Online users </h3>
     <ul class = "list-group" id = "users"> </ul>

     </div>

   </div>
   <div class = "col-md-8">
    <div class="chat" id = "chat"> </div>

    <form id = "messageForm"> 
     <div class = "form-group">
      <label>Enter Message</label>
      <textarea class = "form-control" id = "message"> </textarea>
      <br/>
      <input type="submit" class = "btn btn-primary" value = "Send Message"/>
     </div>
    </form>
   </div>

  </div>

 </div>
 <script>
  $(function(){
   var socket = io.connect();
   var $messageForm = $('#messageForm');
   var $message = $('#message');
   var $chat = $('#chat');
   var $userFormArea = $('#userFormArea');
   var $userForm = $('#userForm');
   var $messageArea = $('#messageArea');
   var $users = $('#users');
   var $username = $('#username');

   $messageForm.submit(function(e){
    e.preventDefault();
    socket.emit('send message', $message.val());
    $message.val('');
   });

   socket.on('new message', function(data){
    $chat.append('<div class = "well"><strong>'+data.user+'</strong>:' + data.msg + '<div>');
   });

   $userForm.submit(function(e){
    e.preventDefault();
    socket.emit('new user', $username.val(),function(data){
     if(data){
      $userFormArea.hide();
      $messageArea.show();
     }
    });
    $username.val('');
   });
   socket.on('get users', function(data){
    var html = '';
    for (i = 0; i< data.length; i++){
     html +='<li class = "list-group-item" >' + data[i]+ '</li>';
    }
    $users.html(html);
   });
  });
 </script>

</body>
</html>

I've been struggling for this for a while, is it something wrong with socket.io? Or is it something that I am doing wrong.

If there's something different I have to do, please let me know. I literally connect to the socket, and it spews out console logs.

As you can see in the picture, I want it to say Connected: 1 sockets connected after ONE person connections.

NotaMan
  • 65
  • 5
  • Do you really write code with one space per indent level? – jfriend00 Nov 14 '17 at 04:21
  • Sexy, clean, and yes. Any idea on the socket's though? – NotaMan Nov 14 '17 at 04:26
  • Are you saying that the very first socket that connects shows `connections.length` as 18324? FYI, `io.sockets.on()` should be `io.on()`. – jfriend00 Nov 14 '17 at 06:00
  • Yup, it literally spams the io.sockets.on('connection'). I changed it to io.on(), same effect. – NotaMan Nov 14 '17 at 20:16
  • When I run your exact server code with your exact HTML page (loaded with `http://localhost:3000`, I see this in the console: `server running ... Connected: 1 sockets connected`. I do not see your issue at all. So, something else is going on that is not only caused by the code you've shown. Makes me wonder if you have an older version of your server running and you aren't actually running this version of your server. You could check processes running or reboot your server and start this one from scratch or change the port you're running on. – jfriend00 Nov 14 '17 at 21:52
  • Are you opening the web page via `http://localhost:3000` or are you opening it directly from the file system? – jfriend00 Nov 14 '17 at 23:37
  • Very similar issue here: https://stackoverflow.com/questions/47296987/socket-io-hundreds-of-requests-instead-of-one-connection/47297234#comment81546584_47297234 which was fixed by reinstalling socket.io server code. – jfriend00 Nov 15 '17 at 00:49

1 Answers1

0

This usually occurs when client and server are using different versions of socket.io. Check the version of server from package.json and give client the same version. I had the same issue and upgrading to socket.io version 2.1.1 fixed it.

Mohsin
  • 692
  • 1
  • 7
  • 15