Top Banner
Uma breve história sobre o tempo com Socket.io & Node.js.
29

Uma breve história sobre o tempo com Socket.io e Node.js

Jul 28, 2015

Download

Internet

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Uma breve história sobre o tempo com Socket.io e Node.js

Uma breve história sobre o tempo com Socket.io & Node.js.

Page 2: Uma breve história sobre o tempo com Socket.io e Node.js

GUILHERMEODERDENGEDesenvolvedor JavaScript na Chute

http://github.com/chiefgui/

Page 3: Uma breve história sobre o tempo com Socket.io e Node.js
Page 4: Uma breve história sobre o tempo com Socket.io e Node.js

PollingInput Output

I/O

Page 5: Uma breve história sobre o tempo com Socket.io e Node.js

Short Polling

Page 6: Uma breve história sobre o tempo com Socket.io e Node.js

The force is with you, young Pinkman, but youare not a Jedi yet.

Page 7: Uma breve história sobre o tempo com Socket.io e Node.js

HIGHLIGHT

Simples implementação.

Page 8: Uma breve história sobre o tempo com Socket.io e Node.js

Long Polling

Page 9: Uma breve história sobre o tempo com Socket.io e Node.js

Is the force with you?

Yes, it is!

Am I your father?

Yes, you are!

Is Angular better than Backbone?

Page 10: Uma breve história sobre o tempo com Socket.io e Node.js

HIGHLIGHTS

✓ Simples implementação;✓ Relativamente econômico;

Você recebe uma resposta quando ela passar a existir.

✓ Relativamente inteligente.

Page 11: Uma breve história sobre o tempo com Socket.io e Node.js

WEBSOCKETSÉ um protocolo (ws://) que provê

um canal de comunicação “full-duplex” sobre uma única conexão TCP.

Page 12: Uma breve história sobre o tempo com Socket.io e Node.js

Comunicação “full-duplex”?

Hey, Pinkman.

What’s up, Mr. Vader?

Page 13: Uma breve história sobre o tempo com Socket.io e Node.js

✓ Relativamente simples implementação;✓ Econômico e inteligente;✓ Funciona na maioria dos browsers modernos;✓ Nasceu para o propósito do tempo-real na web;

Após o handshake feito pelo HTTP, tudo é WebSocket.

HIGHLIGHTS

Page 14: Uma breve história sobre o tempo com Socket.io e Node.js

A implementação deixa de ser relativa.Agora ela é só simples.

Page 15: Uma breve história sobre o tempo com Socket.io e Node.js

npm install ——save socket.io

Page 16: Uma breve história sobre o tempo com Socket.io e Node.js

<script src="/socket.io/socket.io.js"></script><script> var socket = io(); </script>

index.html

Page 17: Uma breve história sobre o tempo com Socket.io e Node.js

GET ‘/index.html’

index.htmlvar socket = io();

Emite um evento “connection”

Page 18: Uma breve história sobre o tempo com Socket.io e Node.js

var io = require ('socket.io')(80);

io.on('connection', function () { console.log(‘a user is connected'); });

'connection'

Built-in Events

disconnect reconnect reconnect_attempt

Page 19: Uma breve história sobre o tempo com Socket.io e Node.js

index.html

Hey, TDC! You rock!

$('form').on('submit', function () { socket.emit('message:send', $('input').val()); });

<script src=“jquery.js"></script> <script src="/socket.io/socket.io.js"></script> <script> var socket = io();

</script>

Page 20: Uma breve história sobre o tempo com Socket.io e Node.js

var io = require ('socket.io')(80);

io.on('connection', function (socket) { console.log(‘a user is connected’);

});

socket.on('message:send', function (message) { io.emit('message:received', message); });

'message:send''message:received'

Page 21: Uma breve história sobre o tempo com Socket.io e Node.js

socket.on(‘message:received', function (message) { $(‘.messages’).prepend('<li>' + message + ‘</li>'); });

Page 22: Uma breve história sobre o tempo com Socket.io e Node.js

.emit

.onI/O

Page 23: Uma breve história sobre o tempo com Socket.io e Node.js

Namespaces

socket.of(‘/chat’).emit(‘message:send’, message);socket.of(‘/analytics’).emit(‘user:click’, axis);

socket.of(‘/chat’).on(‘message:send’, function (message) {});socket.of(‘/analytics’).on(‘user:click’, function (axis) {});

Page 24: Uma breve história sobre o tempo com Socket.io e Node.js

<script> var chatSocket = io(‘/chat’); var analyticsSocket = io(‘/analytics’); </script>

Page 25: Uma breve história sobre o tempo com Socket.io e Node.js

socket.broadcast.emit(‘notification’, something);

socket.broadcast.on(‘notification’, function () {});

socket.broadcast.emit(‘notification’, something);

socket.broadcast.on(‘notification’, function () {});

broadcast emite para todos os clientes dentro de um socket, exceto o emissor.

Page 26: Uma breve história sobre o tempo com Socket.io e Node.js

io.of(‘/chat’).on(‘connection’, function (socket) { socket.broadcast.emit('something', { to: ‘someone’ }); });

io.of(‘/analytics’);

Page 27: Uma breve história sobre o tempo com Socket.io e Node.js

Roomsio.of(‘/chat’).on(‘connection’, function (socket) { socket.join(‘trilha javascript’); });

io.to(‘trilha javascript’).emit(‘message’, { message: ‘Hello!’ });

var socket = io(‘/chat’);

chat.html

Page 28: Uma breve história sobre o tempo com Socket.io e Node.js

DETALHES IMPORTANTES

- Preocupe-se com segurança quando trabalhar com WebSockets;

- Socket.IO é uma opção escalável às técnicas de polling;

- Socket.IO é simples. Mesmo.

Page 29: Uma breve história sobre o tempo com Socket.io e Node.js

http://socket.io/