Speed up development with full-stack environments for every branch.

Learn More

Sending volatile messages [Socket.IO]

522 Runs 2540 Views 547 Copies
Saved

Saved

sundip 66

sundip
published 4 years ago

/**
 * More details here: http://socket.io/#how-to-use 
 */

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

/** Environment-specific port and ip address **/
var port = 80,
    ip = process.env.OPENSHIFT_NODEJS_IP;

server.listen(port, ip);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.set('log level', 1);

function getBieberTweet(cb) {
  cb('check out iambieber.com');
}

io.sockets.on('connection', function (socket) {
  var tweets = setInterval(function () {
    getBieberTweet(function (tweet) {
      socket.volatile.emit('bieber tweet', tweet);
    });
  }, 100);

  socket.on('disconnect', function () {
    clearInterval(tweets);
  });
});
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).