Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome or Safari browser. Firefox 10 (to be released soon) will also handle it.

Node.js and Socket.io Demo in JSCafe

presentation

Presentation is here!!

http://goo.gl/46dk3

Who are you ?

icon

Working at : dena

Twitter : @yosuke_furukawa

Blog : from scratch

Community : node

What is Node.js ?

Javascript on *** (Server, Arduino, Rapsberry PI, etc.)

Not only web browser !!!

Keywords

  1. Non-Blocking IO
  2. Callbacks
  3. Single Thread
  4. process.nextTick
  5. Event Emitter

Non Blocking I/O

printLine("Please input your name: ");
var name = getLine();
var result = db.save(name);
if (result === 'ok')
  printLine("saved!");

reference note: pseudo code.

Non Blocking I/O

printLine("Please input your name: ");
// non blocked this line, go next.
var name = getLine();
// name is *undefined*.
var result = db.save(name);
// result is always not 'ok' !!!
if (result === 'ok')
  printLine("saved!");

Callbacks

printLine("Please input your name: ", function(){
  getLine(function(err, name) {
    db.save(name, function(err, r) {
      if (r === 'ok')
        printLine("saved!");
    });
  });
});

single thread

printLine("Please input your name: ", function(){
  getLine(function(err, name) {
    db.save(name, function(err, r) {
      if (r === 'ok')
        printLine("saved!");
      // add new code
      db.getAll(function(err, names) {
        names.forEach(function(name) {
          printLine(name);
        });
      });
    });
  });
});

single thread

printLine("Please input your name: ", function(){
  getLine(function(err, name) {
    db.save(name, function(err, r) {
      if (r === 'ok')
        printLine("saved!");
      db.getAll(function(err, names) {
        // if num of names is huge.
        // stop all tasks !!!
        names.forEach(function(name) {
          printLine(name);
        });
      });
    });
  });
});

process.nextTick

printLine("Please input your name: ", function(){
  getLine(function(err, name) {
    db.save(name, function(err, r) {
      if (r === 'ok')
        printLine("saved!");
      db.getAll(function(err, names) {
        names.forEach(function(name) {
          // Dont stop all task.
          process.nextTick(function(){
            printLine(name);
          });
        });
      });
    });
  });
});

Crazy callbacks...

printLine("Please input your name: ", function(){
  getLine(function(err, name) {
    db.save(name, function(err, r) {
      if (r === 'ok')
        printLine("saved!");
      db.getAll(function(err, names) {
        names.forEach(function(name) {
          process.nextTick(function(){
            printLine(name);
          });
        });
      });
    });
  });
});

Event Emitter!!

var event = new EventEmitter();
event.on('hoge', function(arg) {
  printLine(arg);
});
event.emit('hoge', 'fuga');
// print fuga

Event Emitter!!

printLine("Please input your name: ", function(){
  getLine(function(err, name) {
    db.emit('save', name);
  });
});
db.on('save end', function(err, r) {
  if (r === 'ok')
    printLine("saved!");
  db.emit('getAll');
});
db.on('getAll end', function(err, names){
  names.forEach(function(name) {
    process.nextTick(function(){
      printLine(name);
    });
  });
});

I dont have time to talk all keywords.

If you have an interest, you would read these to be better.

nyumon

Node.js Patterns and Opinions

Node.jsとは何か(Ryan Dahlの講演)

I guess 10 minutes left...

Play with Node!!!!

Hello JSCafe on HTTP

easy to write!!

var http = require('http');
http.createServer(function(req, res){
  res.end("<h1>hello jscafe.</h1>");
}).listen(3000);

Hello JSCafe on chat with you.

node.js chat!!

var net = require('net');
var clients = [];
var server = net.createserver(function(sock){
  sock.on('end', function(){
    sock.emit('data', "goodbye!! \n");
    clients.splice(clients.indexof(sock), 1);
  });
  sock.resume();
  clients.foreach(function(i){
    sock.pipe(i, {end:false}).pipe(sock, {end:false});
  });
  clients.push(sock);
});
server.listen(1337);

Hello JSCafe on chat with you.

please access

$ nc <IP address> 1337

I guess my greetings reach you.

Node is fun??

"I'm Fan!!"

I guess 5 minutes left....

Socket.io

socket.io

Socket.io is a realtime library for Node.js

Socket.io uses WebSocket, long polling, Adobe flash socket

So Socket.io supports many kinds of browsers!!!

  1. IE 5.5+
  2. Safari 3+
  3. Google Chrome 4+
  4. Firefox 3+
  5. Opera 10.61+

Actually, this slide uses Socket.io :)

A lot of demo use Socket.io

Google "socket.io chat/paint/presentation"

Socket.io is easy to use!!!

var io = require('socket.io').listen(3000);
io.sockets.on('connection', function(socket) {
  socket.on('message', function(message) {
    // broadcast
    io.sockets.emit('message', message);
  });
});

Socket.io + Webhook

Summary

DeNA NEEDS YOU!!!

Use a spacebar or arrow keys to navigate