Mathilde Lemée & Romain Maton
La théorie, c’est bien, la pratique … aussi !
Venez nous rejoindre pour découvrir les profondeurs de Node.js !
Nous nous servirons d’un exemple pratique pour vous permettre d’avoir une premiere experience complete autour de Node.js et de vous permettre de vous forger un avis sur ce serveur Javascript qui fait parler de lui !
http://soft-shake.ch/2011/conference/sessions/incubator/2011/09/01/hands-on-nodejs.html
4. Co-founder of Duchess France
http://www.java-freelance.fr
@MathildeLemee
Creator of Web Tambouille
http://www.web-tambouille.fr
@rmat0n
5. Summary
• What ? Why ?
• Non blocking API example
• Event programming model
• Express, Socket.IO and modules
• Mini Hands On
• Unit Test
• Limits
6. What is Node ?
Server-side Javascript
Node.js javascript implementation is V8 Javascript
Engine (Google Chrome)
Provide an easy way to build scalable network programs
Non blocking I/O
Single Threaded
Written by Ryah Dahl
7. What is Node ?
http://codingrelic.geekhold.com/2010/08/nodejs-from-30000-feet.html
8. What is Node ?
var http = require('http');
http.createServer(function (request, response) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("Hello Worldn");
}).listen(1337, "127.0.0.1");
console.log("Server running at http://127.0.0.1:1337/");
~$ node server.js
~$ curl http://127.0.0.1:1337/
9. Why using Node ?
• Ryan Dahl: "Node.js project: To provide a purely evented,
non-blocking infrastructure to script highly concurrent
programs" (http://yuilibrary.com/theater/ryan-dahl/dahl-node/)
• Scalable software
• Non blocking I/O
• Same language and share code between server side and
client side
• JSON friendly (web, server, database...)
11. Non blocking API example
var data = File.read("file.txt");
...
// Here you have to wait... maybe a lot...
...
// And your thread is still alive... doing nothing...
...
parseResult(data);
12. Non blocking API example
var data = File.read("file.txt", function(data) {
parseResult(data);
});
// Here, your thread is alive and continue working !!!
myOtherCode();
13.
14. Event programming model
• Events are the heart of Node.js
• Everything is event based
• You can create yours own events
23. Socket.IO - Why ?
WebSocket : Firefox 4, Chrome 4, Opera 10.70, and Safari 5.
Asynchronous communication from client to server.
AJAX - XmlHttpRequest ?
Flash / AJAX Long Polling
Disconnection
24. Socket.IO - How
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
io.sockets.emit('info', 'a player join the game');
socket.on('chat', function (msg) {
console.log('send a chat', msg);
});
socket.on('disconnect', function () {
sockets.emit('user disconnected');
});
});
25. Socket.IO - How
<script>
var socket = io.connect('http://localhost');
socket.on('connect', function () {
socket.on('info', function (data) {
alert(data);
});
});
socket.emit('chat',myMessage);
</script>
26. Socket.IO - How
• custom messages :
socket.emit('my custom event',{data:xxx});
• volatile :
socket.volatile.emit('tweet',tweet);
• acknoledgements
• broadcast :
socket.broadcast.emit('hello','world');
• room :
socket.join('justin bieber fans')
• send a message in a room :
io.sockets.in('my room').emit('hello','world');
• storing data :
socket.set('name','Mathilde',function () { //callback });
27. Modules
Create yours !
hello.js
var world = function() {
alert("helloworld");
};
exports.world = world;
server.js
var hello = require("./hello")
hello.world();
29. Tests
MLE
• Unit tests
o Node.js provides its own assert module
http://nodejs.org/docs/v0.5.6/api/assert.html
o QUnit (JQuery) http://docs.jquery.com/Qunit
o NodeUnit https://github.com/caolan/nodeunit
o Expresso https://github.com/visionmedia/expresso
• Integration tests
o Zombie.js http://zombie.labnotes.org
• Behavior Driven Development
o vowsjs http://vowsjs.org/
o jasmine-node https://github.com/mhevery/jasmine-node
• https://github.com/joyent/node/wiki/modules#wiki-testing
30. Unit Tests with QUnit
<script>
$(document)
.ready(
function() {
module("1. Game");
test(
"Connais la valeur du joueur suivant en fonction du joueur actuel",
function() {
game = new Game();
game.take(3);
game.take(1);
equals(game.otherPlayer(), "O", "O est le joueur précédent");
});
...
</script>
32. Limits
- Cryptic error messages
node.js:50 throw e; ^
Error: ECONNREFUSED, Connection refused at
IOWatcher.callback (net:870:22) at node.js:607:9
client.on("error", function (err) {
console.log("Error " + err);
});
33. Limits
- Cryptic error messages
- Only one thread (it is also an advantage)
- Can be difficult to read
- Non async lib/third party decrease perfs
- IDE, Tooling, Debugger, Profiler
- How to choose a good module ?