GeorgeMarkham
GeorgeMarkham

Reputation: 15

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){
   res.send('Loaded');
});

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();
</script> 

Upvotes: 1

Views: 10476

Answers (2)

Zeeshan Cornelius
Zeeshan Cornelius

Reputation: 328

Cordova's white-list plugin does the job for you. It helps you controls which URLs the app is allowed to ask the system to open by making changes to your config.xml file. You would have to add <access origin="YOUR_HOST" /> in your config.xml file.

Upvotes: 1

JavaScript Warrior
JavaScript Warrior

Reputation: 763

Try this answer on SO: https://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.

Regards!

Upvotes: 2

Related Questions