Slide 1

Slide 1 text

Aplicações Realtime com XMPP

Slide 2

Slide 2 text

Rafael Macedo @macedorafael http://github.com/rafaelmacedo

Slide 3

Slide 3 text

Rafael Macedo @macedorafael http://github.com/rafaelmacedo

Slide 4

Slide 4 text

backend…

Slide 5

Slide 5 text

…com chapéu de front

Slide 6

Slide 6 text

…com chapéu de front by @jcemer

Slide 7

Slide 7 text

codeminer42.com

Slide 8

Slide 8 text

Estamos contratando codeminer42.com

Slide 9

Slide 9 text

XMPP

Slide 10

Slide 10 text

EXtensible Messaging and Presence Protocol

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

~ 300 extensões registrados na XSF (XMPP Standards Foundation) http://xmpp.org/xmpp-protocols/xmpp-extensions/

Slide 13

Slide 13 text

XEP (XMPP Extension Protocol) ✴ discussões na lista de email ✴ reviews ✴ votação ✴ testes de interoperabilidade

Slide 14

Slide 14 text

Um pouco de história…

Slide 15

Slide 15 text

Um pouco de história…

Slide 16

Slide 16 text

Um pouco de história… 1999 Projeto Jabber iniciado por Jeremie Miller 2001 Fundação da Jabber Software Foundation (JSF) 2004 Publicação XMPP_1.0 RFC3920 RFC3921 RFC3922 RFC3923

Slide 17

Slide 17 text

Um pouco de história… 2007 Passou a se chamar XMPP Standard Foundation(XSF) 2011 RFC 6121 RFC 6120 RFC 6122

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Arquitetura XMPP

Slide 20

Slide 20 text

Endereçamento

Slide 21

Slide 21 text

XML Stanzas

Slide 22

Slide 22 text

Arquitetura XMPP Servidor Cliente c2s

Slide 23

Slide 23 text

Servidores

Slide 24

Slide 24 text

Clientes

Slide 25

Slide 25 text

✴ Jabber ID (JID) Endereçamento [email protected]/home

Slide 26

Slide 26 text

✴ Jabber ID (JID) Endereçamento [email protected]/home

Slide 27

Slide 27 text

✴ Jabber ID (JID) Endereçamento [email protected]/home

Slide 28

Slide 28 text

✴ Jabber ID (JID) Endereçamento [email protected]/home

Slide 29

Slide 29 text

XML Stanzas ✴ ✴ ✴

Slide 30

Slide 30 text

Hello World from XMPP!

Slide 31

Slide 31 text

Hello World from XMPP!

Slide 32

Slide 32 text

Hello World from XMPP!

Slide 33

Slide 33 text

Hello World from XMPP! chat error normal groupchat headline

Slide 34

Slide 34 text

Hello World from XMPP!

Slide 35

Slide 35 text

away @ RSJS 2014

Slide 36

Slide 36 text

away @ RSJS 2014

Slide 37

Slide 37 text

away @ RSJS 2014

Slide 38

Slide 38 text

away @ RSJS 2014

Slide 39

Slide 39 text

Slide 40

Slide 40 text

Slide 41

Slide 41 text

Slide 42

Slide 42 text

get set result

Slide 43

Slide 43 text

Slide 44

Slide 44 text

Slide 45

Slide 45 text

XMPP na WEB

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

http://strophe.im/strophejs/ Strophe.js An XMPP library for JavaScript Strophe.js is an XMPP library for JavaScript. It is primary purpose is to enable web-based, real-time XMPP applications that run in any browser.

Slide 50

Slide 50 text

✴ Documentada ✴ Otimizada ✴ Testada ✴ Chesspark, Seesmic, Yammer

Slide 51

Slide 51 text

Multi User Chat (MUC)

Slide 52

Slide 52 text

https://github.com/rafaelmacedo/xmpp-chat-rsjs2014

Slide 53

Slide 53 text

✴ XEP-0045: Multi-User Chat ✴ troca de mensagens no contexto de uma sala ✴ banir usuários ✴ moderação ✴ … http://xmpp.org/extensions/xep-0045.html

Slide 54

Slide 54 text

Primeiros passos ✴ Criar conexão ✴ Conectar

Slide 55

Slide 55 text

Criar conexão

Slide 56

Slide 56 text

$("#login").on("click", function() { var connection = new Strophe.Connection( "http://taskie.org:5280/http-bind"); ! var jid = $("#jid").val() , passwd = $("#passwd").val(); ! connection.connect(jid, passwd, function(status) { if (status === Strophe.Status.CONNECTED) { alert("ihuuu connected!!!"); } else { if (status === Strophe.Status.AUTHFAIL) { alert("wrong passwd”); } } .... }); }); !

Slide 57

Slide 57 text

$("#login").on("click", function() { var connection = new Strophe.Connection( "http://taskie.org:5280/http- bind"); ! var jid = $("#jid").val() , passwd = $("#passwd").val(); ! connection.connect(jid, passwd, function(status) { if (status === Strophe.Status.CONNECTED) { alert("ihuuu connected!!!"); } else { if (status === Strophe.Status.AUTHFAIL) { alert("wrong passwd”); } } .... }); }); !

Slide 58

Slide 58 text

Strophe.Connection = function(service, options ) http://strophe.im/strophejs/doc/1.1.3/files/strophe- js.html#Strophe.Connection

Slide 59

Slide 59 text

Strophe.Connection = function(service, options ) http://strophe.im/strophejs/doc/1.1.3/files/strophe- js.html#Strophe.Connection ws:// ou wss:// => WebSocket http:// ou https:// => BOSH

Slide 60

Slide 60 text


 WEBSockets A B Iniciar conexão TCP GET /socket HTTP/1.1 \o/

Slide 61

Slide 61 text

✴xmpp over websocket ✴draft (http://tools.ietf.org/html/ draft-ietf-xmpp-websocket-06) ✴versão 6 (22/04/2014)

Slide 62

Slide 62 text


 BOSH ✴Bidirectional-streams Over Syncrhonous HTTP

Slide 63

Slide 63 text

✴push/pull ✴eficiente ✴latência baixa

Slide 64

Slide 64 text

A B idle

Slide 65

Slide 65 text

A B idle

Slide 66

Slide 66 text

$("#login").on("click", function() { var connection = new Strophe.Connection( "http://taskie.org:5280/http-bind"); ! var jid = $("#jid").val() , passwd = $("#passwd").val(); ! connection.connect(jid, passwd, function(status) { if (status === Strophe.Status.CONNECTED) { alert("ihuuu connected!!!"); } else { if (status === Strophe.Status.AUTHFAIL) { alert("wrong passwd”); } } .... }); });

Slide 67

Slide 67 text

Strophe.Connection = function(service, options ) http://strophe.im/strophejs/doc/1.1.3/files/strophe- js.html#Strophe.Connection connect: function(jid, passwd, callback )

Slide 68

Slide 68 text

Strophe.Connection = function(service, options ) http://strophe.im/strophejs/doc/1.1.3/files/strophe- js.html#Strophe.Connection connect: function(jid, passwd, callback ) connecting authenticating authentication failed connected disconnecting disconnected

Slide 69

Slide 69 text

Strophe.Status .CONNECTING .AUTHENTICATING .CONNECTED .DISCONNECTING .DISCONNECTED .CONNFAIL .AUTHFAIL ! !

Slide 70

Slide 70 text

$("#login").on("click", function() { var connection = new Strophe.Connection( "http://taskie.org:5280/http-bind"); ! var jid = $("#jid").val() , passwd = $("#passwd").val(); ! connection.connect(jid, passwd, function(status) { if (status === Strophe.Status.CONNECTED) { alert("ihuuu connected!!!"); } else { if (status === Strophe.Status.AUTHFAIL) { alert("wrong passwd”); } } .... }); });

Slide 71

Slide 71 text

Conectado!! hora de entrar em alguma sala

Slide 72

Slide 72 text

rsjs2014 A B C D

Slide 73

Slide 73 text

rsjs2014 A B C D A B C

Slide 74

Slide 74 text

rsjs2014 A B C D D D D D

Slide 75

Slide 75 text

Slide 76

Slide 76 text

Slide 77

Slide 77 text

Slide 78

Slide 78 text

Slide 79

Slide 79 text

$("#join").on("click", function() { var room = $("#room"), , nick = $("#nick"); ! var presence = new Strophe.Builder(“presence”, { to: room + “@conference.taskie.org/” + nickname }) .c("x", {xmlns: "http://jabber.org/protocol/muc"}); ! connection.send(presence); }); Enviando informação de presença

Slide 80

Slide 80 text

$("#join").on("click", function() { var room = $("#room"), , nick = $("#nick"); ! var presence = new Strophe.Builder(“presence”, { to: room + “@conference.taskie.org/” + nickname }) .c("x", {xmlns: "http://jabber.org/protocol/ muc"}); ! connection.send(presence); }); Enviando informação de presença

Slide 81

Slide 81 text

Strophe.Builder = function(name, attrs ) http://strophe.im/strophejs/doc/1.1.3/files/strophe- js.html#Strophe.Builder

Slide 82

Slide 82 text

new Strophe.Builder( "message", { to: "[email protected]", type: "chat" } );

Slide 83

Slide 83 text

$pres(attrs) $msg(attrs) $iq(attrs)

Slide 84

Slide 84 text

new Strophe.Builder( "message", { to: "[email protected]", type: "chat" } ); $msg( { to: "[email protected]", type: "chat" });

Slide 85

Slide 85 text

c: function(name, attrs, text ) t: function(text) adicionando nó filho adicionando conteúdo

Slide 86

Slide 86 text

new Strophe.Builder( "message", { to: "[email protected]", type: "chat" } ); $msg( { to: "[email protected]", type: "chat" }) .c("body") .t("Hello World!");

Slide 87

Slide 87 text

$("#join").on("click", function() { var room = $("#room"), , nick = $("#nick"); ! var presence = $pres( { to: room + “@conference.taskie.org/” + nickname }) .c("x", {xmlns: "http://jabber.org/protocol/muc"}); ! connection.send(presence); }); Enviando informação de presença

Slide 88

Slide 88 text

$("#join").on("click", function() { var room = $("#room"), , nick = $("#nick"); ! var presence = $pres( { to: room + “@conference.taskie.org/” + nickname }) .c("x", {xmlns: "http://jabber.org/protocol/muc"}); ! connection.send(presence); }); Enviando informação de presença

Slide 89

Slide 89 text

Strophe.Connection = function(service, options ) http://strophe.im/strophejs/doc/1.1.3/files/strophe- js.html#Strophe.Connection connect: function(jid, passwd, callback ) send: function(elem)

Slide 90

Slide 90 text

Entrei?! mas cade todo mundo????

Slide 91

Slide 91 text

Recebendo informação de presença connection.addHandler(onPresence, null, "presence");

Slide 92

Slide 92 text

Strophe.Connection = function(service, options ) http://strophe.im/strophejs/doc/1.1.3/files/strophe- js.html#Strophe.Connection connect: function(jid, passwd, callback ) send: function(elem) addHandler: function (handler, ns, name, type, id, from, options )

Slide 93

Slide 93 text

addTimedHandler: function(period, handler ) deleteTimedHandler: function( handRef) deleteHandler: function(handRef)

Slide 94

Slide 94 text

onPresence: function(presence) { var $presence = $(presence) , presenceType = $presence.attr("type"); var from = $presence.attr("from") , room = Strophe.getBareJidFromJid(from); , nick = Strophe.getResourceFromJid(from); if (presenceType === "error") { connection.disconnect(); } else if (presenceType !== "unavailable") { participants.push({ nickname: nick }); } if (presenceType !== "error") { if ($presence.find("status[code='110']").length > 0) { if ($presence.find("status[code='210']").length > 0) { nick = Strophe.getResourceFromJid(from)); } } } return true; };

Slide 95

Slide 95 text

onPresence: function(presence) { var $presence = $(presence) , presenceType = $presence.attr("type"); var from = $presence.attr("from") , room = Strophe.getBareJidFromJid(from); , nick = Strophe.getResourceFromJid(from); if (presenceType === "error") { connection.disconnect(); } else if (presenceType !== "unavailable") { participants.push({ nickname: nick }); } if (presenceType !== "error") { if ($presence.find("status[code='110']").length > 0) { if ($presence.find("status[code='210']").length > 0) { nick = Strophe.getResourceFromJid(from)); } } } return true; };

Slide 96

Slide 96 text

[email protected]/rafaelmacedo resource

Slide 97

Slide 97 text

[email protected]/rafaelmacedo Bare JID

Slide 98

Slide 98 text

onPresence: function(presence) { var $presence = $(presence) , presenceType = $presence.attr("type"); var from = $presence.attr("from") , room = Strophe.getBareJidFromJid(from); , nick = Strophe.getResourceFromJid(from); if (presenceType === "error") { connection.disconnect(); } else if (presenceType !== "unavailable") { participants.push({ nickname: nick }); } if (presenceType !== "error") { if ($presence.find("status[code='110']").length > 0) { if ($presence.find("status[code='210']").length > 0) { nick = Strophe.getResourceFromJid(from)); } } } return true;

Slide 99

Slide 99 text

var from = $presence.attr("from") , room = Strophe.getBareJidFromJid(from); , nick = Strophe.getResourceFromJid(from); if (presenceType === "error") { connection.disconnect(); } else if (presenceType !== "unavailable") { participants.push({ nickname: nick }); } if (presenceType !== "error") { if ($presence.find("status[code='110']").length > 0) { if ($presence.find("status[code='210']").length > 0) { nick = Strophe.getResourceFromJid(from)); } }

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

Enviando mensagens var message = $msg( { to: "[email protected]", type: "groupchat" }); message.c("body").t("\o/"); connection.send(message);

Slide 102

Slide 102 text

Ouvindo mensagens connection.addHandler( onPublicMessage, null, "message", "groupchat");

Slide 103

Slide 103 text

onPublicMessage: function(message) { var $message = $(message); var from = $message.attr("from") , room = Strophe.getBareJidFromJid(from) , nick = Strophe.getResourceFromJid(from); if (room === "[email protected]") { var msg = $message.children("body").text(); } return true; }

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

XMPP é a escolha certa? ✴ informação de presença ✴ notificações sem pooling ✴ custom payloads ✴ encriptação de canal

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

Obrigado Rafael Macedo @macedorafael rafaelmacedo.com

Slide 108

Slide 108 text

Obrigado Rafael Macedo @macedorafael rafaelmacedo.com ?

Slide 109

Slide 109 text

Referencias Professinal XMPP- Programming with JavaScript and jQuery XMPP - The Definitive Guide

Slide 110

Slide 110 text

Procurando por salas ✴ iq stanza ✴ disco#items protocol

Slide 111

Slide 111 text

var iq = $iq( { to: "conference.taskie.org", type: "get", id: "rooms-info" }); ! iq.c("query", { xmlns: "http://jabber.org/protocol/ disco#items" }); connection.send(iq);

Slide 112

Slide 112 text

connection.addHandler( onRoomsInfo, null, "iq", "result", "rooms-info");

Slide 113

Slide 113 text

onRoomsInfo: function(iq) { var $IQ = $(iq); var items = $IQ.find("item") , rooms; ! rooms = _.map(items, function(item) { var $item = $(item); ! return { jid: $item.attr("jid"), name: $item.attr("name") }; }); ! return true; }

Slide 114

Slide 114 text

No content