whyamiwearingsocks whyamiwearingsocks - 1 year ago 118
Node.js Question

Connect to a socket.io server from phonegap

I need a way to connect to a socket.io server from a universal (or at least ios and android) phonegap application.

In the browser I would serve the html page from the node script which would allow me to access the socket.io client js file, but I can't do that with phonegap as the html must be packaged to run on the phone.

After doing some research it seems that I need to remotely access the socket.io client js but every attempt at this results in an error.
Here is the server code (Heavily based on the example code supplied by http://socket.io/get-started/chat/)

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

app.get('/', function(req, res){

io.on('connection', function(socket){
console.log("A person connected!");

http.listen(8080, function(){
console.log("listening on port 8080...");

And here is the client code without the added phonegap scripts and stuff:

<script type="text/javascript" src="socket-io/socket-io.js"></script>
<script type="text/javascript">
var socket = io.connect();

Answer Source

Try this answer on SO: http://stackoverflow.com/a/10886574/2516585

Basically you need to edit config.xml in phonegap, and add <access origin="HOST*" />, where HOST is your server's hostname, and then connect to that hostname with socketio.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download