Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 783
Rodrigo Fonseca Avatar asked Aug 23 '14 17:08

Rodrigo Fonseca


People also ask

Does Socket.IO work on iOS?

Socket.IO is a framework that makes it easy to implement Socket and the available for iOS, Android, back-end and front-end. In this article you will find some code in Swift (iOS) and Javascript (Web) for implementing the client and NodeJS for implementing the back-end.

How does Socket work in iOS?

The socket communication relies on the client-server logic, where a persistent connection between a server and a client always exists. To be more precise, the server “opens” a dedicated port where clients get connected to it.

Does Socket.IO work on mobile?

The first step is to install the Java Socket.IO client with Gradle. We must remember adding the internet permission to AndroidManifest. xml . Now we can use Socket.IO on Android!

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).


1 Answers

I made it, this tutorial is very good but it's not totally right.

You have to connect the socket to your server first (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);
            });
        });
    });
like image 200
Rodrigo Fonseca Avatar answered Sep 28 '22 08:09

Rodrigo Fonseca