Help! Looking for a working socket.io sample with Phonegap Build

958 views
Skip to first unread message

Frédéric Mériot

unread,
Aug 1, 2013, 4:27:45 PM8/1/13
to phon...@googlegroups.com
I'am using Phonegap-Build for creating a small android application. I'd like to use Socket.io.

My server (to comunicate with) is a node app hosted at Heroku.com. 

=========== SERVER SIDE =========== 

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


//This config is mandatory for heroku
io
.configure(function (){
io
.set("log level",1);
io
.set("transports", ["xhr-polling"]);
io
.set("polling duration", 10);
});

var allowCrossDomain = function(req, res, next) {
res
.header("Access-Control-Allow-Origin", "*");
res
.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
}

express
.configure(function () {
express
.set('port', process.env.PORT || 8080);
express
.use(allowCrossDomain);
express
.use("/", app.static("./wwwroot/"));
});

io
.on('connection', function(socket) {
console
.log(socket.id + " is connected");
socket
.emit('connected', 'You welcome!');
});

server
.listen(express.get('port'));
console
.log("Express server is listening on port " + express.get('port'));



=========== CLIENT SIDE =========== 

<script src="./js/socket.io.js"/>
<script src="cordova.js"/>
<script>
var socket;
document
.addEventListener('deviceReady',ondeviceReady,false);


function onDeviceReady(){
    connect
();
}


function connect(){
    socket
= io.connect('http://testapp.herokuapp.com');
    socket
.on('connected',function(){
        alert
("connected ! ");
   
});
}


</script>



So, this works fine in my browser. But when I build it for androïd app with Phonegap-Build, nothing happens when I launch the app ... 

Please help. Can you give me a simple example of socket.io communication in phonegap ?

Thank you.

Benn

unread,
Dec 10, 2013, 5:31:57 PM12/10/13
to phon...@googlegroups.com
Hi Frédéric,

I have the same issue building my android app. Did you manage to resolve this?
If so how did you get it to work?

Cheers
Benn

Daniel Albuquerque

unread,
Dec 17, 2015, 11:00:47 AM12/17/15
to phonegap

For android you have to edit cordova.xml and add access to the socketio host:

<access origin="HOST*"/> 


You have to add the socketio host to the "ExternalHosts" key in PhoneGap.plist.

See Faq:

Q. Links to and imported files from external hosts don't load?

A. The latest code has the new white-list feature. If you are referencing external hosts, you will have to add the host in PhoneGap.plist under the "ExternalHosts" key. Wildcards are ok. So if you are connecting to "http://phonegap.com", you have to add "phonegap.com" to the list (or use the wildcard "*.phonegap.com" which will match subdomains as well). (Note: If you open the plist file in Xcode, you won't need to fiddle with the XML syntax.)

For android you have to edit cordova.xml and add access to the socketio host:

<access origin="HOST*"/> 

index.html (with socketio example):

...
<script src="HOST/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('HOST');
    socket.on('news', function (data) {
        socket.emit('my other event', { my: 'data' });
    });
</script>
...

app.js (server side javascript / basic socketio example):

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {

socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

The HOST you have to replace with hostname of your socket.io server!

Reply all
Reply to author
Forward
0 new messages