Socket.io

by Severin Ibarluzea and Joey Lee

Socket.io

  • Node.js
  • Real-time web communication
  • Faster than polling
  • Simple API

Polling?

Querying web server constantly for new data

// Constantly query web server for updates
setInterval(function(){
    $.ajax({ url: '/mypage', success: function(data){
        // Do stuff with message
    } });
}, 100);

Long Polling

function load(){
    $.ajax({ url: '/mypage', success: function(){
        // do something with the data
    }, complete: load, timeout: 20000 });
}

The Problem

Polling is slow

Simulating sockets from scratch is tricky

Easy configuration

Easy to use server

Installation

npm install socket.io

The Basics

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    // Do stuff with data
  });
</script>

Client

Server

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

server.listen(80);

io.on('connection', function (socket) {
  socket.emit('news', "Hello World!");
});

Wait but I use PHP

Let's talk about the real problem

jk, but WebSockets are still great

http://socketo.me/

Copy of deck

By Severin Ibarluzea