Sam
Sam

Reputation: 47

Socket IO chat example is slow

I'm new to Node.js and Socket.IO and I wanted to try the example explained at

https://socket.io/get-started/chat/

I did everything I had to do and it works: I opened two tabs and messages appear in both clients, but for some reason they appear after 5/6 seconds (sometimes even later). Do you guys know why (I'm using windows 10)?

This is the index.js file code:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;

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

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log(msg)
  socket.emit('chat message', msg);
  });
});

http.listen(port, function(){
  console.log('listening on *:' + port);
});

this is html code:

<!doctype html>
<html>

<head>
    <title>Socket.IO chat</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font: 13px Helvetica, Arial;
        }

        form {
            background: #000;
            padding: 3px;
            position: fixed;
            bottom: 0;
            width: 100%;
        }

        form input {
            border: 0;
            padding: 10px;
            width: 90%;
            margin-right: .5%;
        }

        form button {
            width: 9%;
            background: rgb(130, 224, 255);
            border: none;
            padding: 10px;
        }

        #messages {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }

        #messages li {
            padding: 5px 10px;
        }

        #messages li:nth-child(odd) {
            background: #eee;
        }

        #messages {
            margin-bottom: 40px
        }
    </style>
</head>

<body>
    <ul id="messages"></ul>
    <form action="">
        <input id="m" autocomplete="off" />
        <button>Send</button>
    </form>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>

    <script>
        var socket = io();

        $(function () {
            $('form').submit(function () {
                socket.emit('chat message', $('#m').val());
                $('#m').val('');
                return false;
            });
            socket.on('chat message', function (msg) {
                $('#messages').append($('<li>').text(msg));
                window.scrollTo(0, document.body.scrollHeight);
            });
        });
    </script>
</body>

</html>

this is package.json:

{
    "name": "socket-example",
    "version": "0.0.1",
    "description": "my first socket.io app",
    "dependencies": {
        "engine.io": "^3.1.5",
        "express": "^4.15.2",
        "socket.io": "^2.0.4"
    }
}

Upvotes: 2

Views: 4906

Answers (1)

Keith
Keith

Reputation: 24231

Seems to be a known problem ->

https://github.com/socketio/socket.io/issues/3179

So in your index.js file..

change ->

var io = require('socket.io')(http);

to ->

var io = require('socket.io')(http, { wsEngine: 'ws' });

Doing this I now get instant feedback, running windows 10..

Upvotes: 4

Related Questions