3
votes

I'm using Socket.io on the Titanium framework via https://github.com/nowelium/socket.io-titanium. So far, that "port" only supports the XHR-polling transport. In the past, I've had success with Socket.io using the Websocket transport.

The problem I'm having now is that my socket connection seems to "drop" every 10 seconds for a few seconds at a time. This means chat messages are dropped, etc. Is this expected behavior with XHR-polling - do I need a implement a queue system - or is there some way I can look to fix this issue?

   debug - setting poll timeoutdebug - discarding transport
   debug - cleared close timeout for client 407473253144647189
   debug - clearing poll timeout
   info  - transport end
   debug - set close timeout for client 407473253144647189
   debug - cleared close timeout for client 407473253144647189
   debug - discarding transport
   debug - client authorized
   info  - handshake authorized 4149191422068834219
   debug - setting request GET /socket.io/1/xhr-polling/4149191422068834219?t=Thu%20Jan%2012%202012%2022%3A37%3A47%20GMT-0800%20%28PST%29
   debug - setting poll timeout
   debug - client authorized for 
   debug - clearing poll timeout
   debug - xhr-polling writing 1::
   debug - set close timeout for client 4149191422068834219
Connection
   debug - setting request GET /socket.io/1/xhr-polling/4149191422068834219?t=Thu%20Jan%2012%202012%2022%3A37%3A47%20GMT-0800%20%28PST%29
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client 4149191422068834219
Last login: Fri Jan 13 00:04:14 on ttys003
1
What platforms are you running this on?Dawson Toth

1 Answers

1
votes

Why don't you load socket.io.js in a web view and pump events via Ti.App.fireEvent / addEventListener? That gives you WebSockets, which doesn't have the limitations of polling.

<html>
<head>
    <script src="http://63.10.10.123:1337/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://63.10.10.123:1337');
        socket.on('onSomething', function (data) {
            Ti.App.fireEvent('onSomething', data);
        });
        Ti.App.addEventListener('emitSomething', function (data) {
            socket.emit('emitSomething', data);
        });
    </script>
</head>
<body>
</body>
</html>

EDIT: I want to note that I did this in a project, and it was crashing my app very consistently on iOS. I looked around, and other devs were hitting this too even without using Titanium. I wouldn't recommend taking this approach, or at the very least, testing it very thoroughly (especially backgrounding and resuming the app). Instead, I use Appcelerator's TCP sockets with my own light protocol to stream data from client to server and server to client.