Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS + SocketIO pushing to Mobile App

I see on SocketIO's web site that it's possible to use SocketIO for mobile device. I tried to find a documentation on its site about how to do this but I could not find any.

Has anyone figured out how to emit a message to mobile device using SocketIO? My guess is that it has to live inside a WebView or something that can run javascript?

EDIT What if I am not planning to use Sencha or PhoneGap. I wanna go native. Objective-C/Java. Would this be possible?

Adding to the chosen Answer I found a java socket.io client https://github.com/benkay/java-socket.io.client

like image 660
denniss Avatar asked Apr 10 '12 04:04

denniss


1 Answers

Socket.io which leverages web socket protocol (An upgrade request sent to server by the client over HTTP) offers a full duplex channel for communication. It is being supported by many web browsers in mobile space as you mentioned.

To implement this, one of the use case would be, for instance you having a phonegap based app to be developed which is basically HTML, CSS & JS. So to have a dedicated full duplex communication channel you can use socket.io.

When you write your Node JS Server : Referring the Socket.io Website

var io = require('socket.io').listen(80); // beauty is web socket still runs in 80/443(WSS) and leverages TCP's capabilities.

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

But in your mobile dev side, you will have to include the JS in your HTML where you wanted to leverage web socket's capabilities.

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

A downloadable client JS is available in the socket.io website.

Hope this helps.

like image 127
Futur Avatar answered Sep 24 '22 02:09

Futur