instant messaging with intel xdk receiving notification on new message

1.2k views Asked by At

i would like to create instant messaging in cross platform. how can i get the application keep listening to the server so when there is a message coming, the application could receive a notification. maybe like service in android? I've read about push message (push mobi) but it doesn't seem to meet my need since it blast the notification on all registered id from admin panel, not from 1 id to another id. i notice GCM but some say it is not suitable for sending and receiving chat.

1

There are 1 answers

7
kindasimple On BEST ANSWER

Sounds like a good scenario for websockets. There's a phongap plugin for android that will allow you to use them.

Take a look at the plugin demo. It looks pretty strait-forward.

client side javascript:

  var socket = io.connect("http://10.0.2.2:8080");

  document.getElementById('log').innerHTML = "connecting";

  socket.on('ping', function (data) {
    document.getElementById('log').innerHTML = data.message;
    socket.emit('pong', { message: 'Hello from client!' });
  });

  socket.on('connect', function () {
     document.getElementById('log').innerHTML = "connected";
  });
});

Server side web service in node.js:

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

io.sockets.on('connection', function (socket) {
  console.log('emit...');
  socket.emit('ping', { message: 'Hello from server ' + Date.now() });
  socket.on('pong', function (data) {
    console.log(data.message);
  });
});

console.log('listening on port 8080');