Rodrigo Fonseca Rodrigo Fonseca - 15 days ago 7
Node.js Question

using socket.io with cordova and IOS device

I'm trying to use this simple tutorial:

http://socket.io/socket-io-with-apache-cordova/


My node.js is working fine and i'm emulating to iOS without problem, but the socket.io isn't working, here is my javascript(the same way as the tutorial above):


app.initialize();

document.addEventListener('deviceready', function() {
console.log(socket);
socket.on('connect', function() {
socket.on('text', function(text) {
alert(text);
});
});
});



and one more thing how can i get this console.log to debug?

here is how i'm getting socket.io(the same way as the tutorial above):

<script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>


here is my server.js(the same way as the tutorial above):

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

io.sockets.on('connection', function (socket) {
console.log('socket connected');

socket.on('disconnect', function () {
console.log('socket disconnected');
});

socket.emit('text', 'wow. such event. very real time.');
});

server.listen(3000);


I think, that the problem and the tutorial didn't told is how i would connect my cordova app with port 3000

Answer

I made it, this tutorial is very good but it's not totally right, because will have to connect your socket to your server first, so you've to connect it to your serve like this(i'm using localhost and port 3000, but if you're using some server outside, i think you've to just put the ip and port):

var socket = io.connect('http://localhost:3000');

and after that, you call "socket.io", here is my complete code:

document.addEventListener('deviceready', function() {
        var socket = io.connect('http://localhost:3000');
        socket.on('connect', function() {
            socket.on('text', function(text) {
                alert(text);
            });
        });
    });