3

EDIT 2:

I observed the same behavior using server-sent events (SSE). See example here. (Of course, it is one way). However, it seems more robust on my Nexus 5 and it doesn't lag frequently. Also it recovers/reconnects fast.


EDIT 1:

See also Websocket interval... on SO.


I'm using a Nexus 5 with Chrome 45 to communicate to my laptop via wifi through the router.

The server on my desktop is coded with node.js version 0.12.7 and I use socket.io 1.3.6. Here is the code for the server:

'use strict'
const app = require( 'express' )();

app.get( '/', function( req, res ){
    res.render( 'test_socketio_client.ejs' );
});

let httpServer = app.listen( 8000 );

const io = require( 'socket.io' )( httpServer );
let i = 0;

io.on( 'connection', function( socket ){
    socket.on( 'req', function( data ){
        console.log( data );
        socket.emit( 'res', {res:'From server', i:i.toString()} );
        ++i;
    });
});

Here is the code for my client:

<html>
<head>
    <meta charset="utf-8"/>
    <title>Test Socket.io</title>
</head>
<body>
    <h1>Test Socket.io</h1>
    <hr/>
    <p>From server: <span id="test1"></span></p>

    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io();
        var i = 0;

        var f = function(){
                socket.emit( 'req', {req:'From client', 
                    i:i.toString()} );
            };

        socket.on( 'res', function( data ){
            $("#test1").html( data.i );
            ++i;
            setTimeout( f, 1000 );
        });

        f();

    </script>

</body>
</html>

If I use the browser on my desktop with http://localhost:8000 there is no problem but when I use my Nexus 5/Chrome 45 through my home wifi network, socket.io seems to behave by "chunks". As you see, my client/server perform a "ping pong" game, emitting a new message after 1 second. The fact is that I receive the ~10 first messages correctly (i.e. i increases from 0 to 10 in ten seconds) then it blocks for few seconds, then it resumes to transmit. Note that I keep, of course, the page on the top on my phone, it doesn't enter in pause state (?)

My question is: how to avoid that, to make sure an emit will be sent over the web socket as soon as it is called? Did I miss something with socket.io setups? Do I ignore something about web sockets and this behavior is normal?

Any idea?

Thanks!

Community
  • 1
  • 1
dom_beau
  • 2,437
  • 3
  • 30
  • 59
  • 2
    well, i have a mobile music app that is remote-controlled by sockets and yeah, sometimes i'll click next(), wait, then click again 3 or 4 times and nothing happens, then a few seconds later it jumps 4 tracks ahead. i don't know if it's the phone, power management, the wifi, the router, or something else, but mobile websockets don't seem to be always on and ready like desktops are. i do get the messages, they just stall and cluster instead of smoothly streaming out as they are emit()ed by the server... oh, and i'm not using socket.io either... – dandavis Sep 16 '15 at 02:52
  • @dandavis Thanks for the info. Maybe someone has an idea about some setups? – dom_beau Sep 16 '15 at 03:18
  • Keep in mind that SSE uses HTTP and not sockets. And about the sockets - also keep in mind that browsers' implementation on phones is pretty different, and websockets have always been a kind of problematic on different phones. Search online for some charts with implementation level and stability. Also enable debugging on phone and start logging timestamped events on when you emit, so that you're sure it's not your browser's fault to delay things. – Andrey Popov Sep 17 '15 at 07:21
  • @AndreyPopov Thanks for the hint. Well I see the difference between SSE and websocket. Actually I wanted to use web sockets to enable the server to notify the browser and to enable the browser to notify the server. But I think I will go to SSE/AJAX rather if these are more stable on mobile devices. – dom_beau Sep 17 '15 at 11:17
  • It would be nice if you can investigate a little bit further on, I'm also interested in this :) – Andrey Popov Sep 17 '15 at 11:22
  • 1
    @AndreyPopov It would be nice of course! :-) But my work is more practical than theoretical and I must find an other way to reach my goal. I simply guest the mobile device cannot (maybe by design) keeps a socket connected for long time over wifi??? – dom_beau Sep 17 '15 at 15:57

1 Answers1

1

I will answer my own question...

I tried both websockets with even only server-to-client communication and server-sent events on my mobile phones (Nexus 5 with Lollipop 5.1 and an old LG p930 with Gingerbread 2.3.5) and none of them works fine with these technos. The communication is broken after 5 ... 10 or 60 seconds but it always breaks.

I will use AJAX with repetitive queries. With a query at each 0.5 sec, I never lost the communication. It will be enough for my current project.

But I would have appreciated to see SSE and/or websockets working on Chrome 45/Android 5.1.

dom_beau
  • 2,437
  • 3
  • 30
  • 59