Professional XMPP Programming with JavaScript and jQuery. Wiley

P r o f e s s i o n a l

XMPP P r o g r a m m i n g w i t h

J a v a S c r i p t ® a n d j Q u e r y

Jack Moffitt

Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com


About the Author
Jack Moffitt is a hacker and entrepreneur based in Albuquerque, New
Mexico. He has founded several startups built on XMPP technology including
Chesspark, a real-time, multi-user gaming platform, and Collecta, a real-time search engine for the Web. He has started and contributed to numerous XMPP related open source and free software projects including the
Strophe XMPP client libraries, the Punjab XMPP connection manager, the
Palaver multi-user chat component, the Speeqe group chat application. He
also has served several terms on both the XSF Board of Directors and the
XSF Council. Previous to his XMPP work, he created the Icecast streaming media server, managed
the Ogg, Vorbis, and Theora codec projects, and co-founded the Xiph.org Foundation, a standards
organization for royalty-free multimedia technologies for the Internet. He is passionate about free
software and open source, open standards, and Internet technology. His favorite programming languages
include JavaScript, Erlang, and Python. You can find him at http://metajack.im, blogging
about start-ups and code, as @metajack on Twitter and Identica, or often spreading the word of
XMPP at technology conferences.

Acknowledgments
We all stand on the shoulders of giants, and I am fortunate to have stood on many friendly
ones throughout my career and while writing this book. Thanks to Carol Long and Ed Connor for
the encouragement, handholding, reminders, and patience that every author needs. Thanks also to
Jason Salas who not only encouraged me on this project but made the appropriate introductions.
Thanks also to Dave Cridland for his work ensuring the technical quality of this book and his tireless
humor. I’m hugely indebted to Peter Saint-Andre, patron saint of XMPP, and the rest of the
XMPP Standards Foundation members for their advice, criticism, and friendship over the years.
Finally, the biggest thanks of all to my wife; not only did she encourage me in this project and put up
with my long hours and absence, she also worked hard as my first reader and made many helpful
suggestions to the text.


Introduction

XMPP powers a wide range of app lications including instant messaging, multi-user chat, voice
and video conferencing, collaborative spaces, real-time gaming, data synchronization, and even
search. Although XMPP started its life as an open, standardized alternative to proprietary instant
messaging systems like ICQ and AOL Instant Messenger, it has matured into an extremely robust
protocol for all kinds of exciting creations.

Facebook uses XMPP technology as part of its chat system. Google uses XMPP to power Google
Talk and its exciting new Google Wave protocol.
Collecta has built a real-time search engine based extensively on XMPP’s publish-subscribe system. Several web browsers are experimenting with XMPP as the basis of their synchronization and sharing systems. Dozens of other companies have XMPP-enabled their web applications to provide enhanced user experiences and real-time interaction.

The core of XMPP is the exchange of small, structured chunks of information. Like HTTP, XMPP
is a client-server protocol, but it differs from HTTP by allowing either side to send data to the other
asynchronously. XMPP connections are long lived, and data is pushed instead of pulled.
Because of XMPP’s differences, it provides an excellent companion protocol to HTTP. XMPP powered

web applications are to AJAX what AJAX was to the static web site; they are the next level of interactivity and dynamism. Where JavaScript and dynamic HTML have brought desktop application features to the web browser, XMPP brings new communications possibilities to the Web.
XMPP has many common social web features built in, due to its instant messaging heritage.
Contact lists and subscriptions create social graphs, presence updates help users keep track of who
is doing what, and private messaging makes communication among users trivial. XMPP also has
nearly 300 extensions, providing a broad and useful range of tools on which to build sophisticated
applications. With only a handful of these, along with the core protocol, amazing things can be built
This book teaches you to harness the promise of XMPP in your own applications, enabling you to
build applications that are social, collaborative, real time, or all of the above. You will develop a
series of increasingly sophisticated XMPP applications, starting from “Hello, World!” and finishing
with a collaborative text editor, a shared sketch pad, and a real-time, multi-player game. By the end,
you will have all the tools you need to build the next generation of applications using XMPP or to
add new real-time, push, or social features to your current applications.

Who This Book Is For
This book is written for developers interested in making XMPP applications. You need not have
any previous experience with XMPP, although it will certainly be helpful if you do. The book starts
from the assumption that you’ve heard great things about XMPP and are looking to dive right in.
The JavaScript language is used to develop all the applications in the book because it is an easy language
to understand, is familiar to a large number of programmers, and comes on every computer
with a web browser. Even though this book uses JavaScript, all the concepts and applications could
be developed in any language; most of the “hard parts” are not related to the programming language,
the libraries used, or the web browser. You do not need to be a JavaScript expert to understand and
work with the code in this book.

It is assumed that you understand the basic front-end web technologies, CSS and HTML. If you’ve
ever written a little HTML from scratch and changed a few CSS styling properties, you should be fine.

This book also makes use of two libraries, jQuery and Strophe. It is helpful if you have used
jQuery before, but if you haven’t, a short primer is included in Appendix A. The Strophe library is
explained fully as the applications are developed.

How This Book Is Structured
This book is primarily organized as a walkthrough tutorial of a series of example XMPP applications.
Each application increases in difficulty and teaches you one or more useful parts of the XMPP
protocol and its extensions. These applications are stripped down for clarity, but they are examples
of the kinds of applications XMPP developers create every day.
This book is divided into three parts.
The first part is an introduction to the XMPP protocol, its uses, and XMPP application design.
Chapter 1 covers the use cases for XMPP, the history of the protocol, and its component parts. 
Chapter 2 explains when XMPP is a good choice for the job and goes into detail about how XMPP applications work, particularly for the Web.
The second part is the meat of the book and contains nine XMPP 
applications that solve a variety of problems. 
Each application is more complex than the last and builds on the concepts of the previous ones. 
Chapter 3 starts with a simple “Hello, World!” type example, and by Chapter 11 you build a
real-time, multi-player game.
The last part covers a few advanced but important topics. Chapter 12 discusses attached sessions, a
useful trick for security, optimization, and persistence. Chapter 13 goes into detail about how best
to deploy and scale XMPP-based applications. Chapter 14 explains how to use Strophe’s plug-in system and how to create your own plug-ins.

What You Need to Use This Book
This book makes use of web technologies and therefore requires almost no special tools. You can
use, build, and run the applications in this book on virtually any platform. 
The libraries needed for the applications are explained in Chapter 3, and most can be used without downloading any code. You will need some way to serve web pages such as a local web server or a hosting account somewhere.

If you don’t have these readily available, you can use the Tape program to serve the files; Tape
is a simple web server and is explained in Appendix B. It is an unfortunate requirement of browser
security policy that you can’t easily run these applications directly from your local file system.
You will need an XMPP account (or multiple accounts in some cases if you want to test the code
by yourself) to run the applications. You can avail yourself of any of the public XMPP servers for
this purpose, although you will need to ensure that the server has support for publish-subscribe and
multi-user chat; most do. You can also download and run your own XMPP server instead, although
this is not covered in the book.



Screenshot


Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 12,912 KB
 Pages
 484 p
 File Type
 PDF format
 ISBN
 978-0-470-54071-8
 Copyright
 2010 by Wiley Publishing, Inc 


Contents
Introduction xIx
XMPP Protocol a Part I: nd Architecture
Chapter 1: Getting to Know XMPP 3
What Is XMPP? 4
A Brief History of XMPP 5
The XMPP Network 6
Servers 6
Clients 7
Components 7
Plug-ins 8
XMPP Addressing 8
XMPP Stanzas 9
Common Attributes 10
Presence Stanzas 1 1
Message Stanzas 13
IQ Stanzas 15
Error Stanzas 17
The Connection Life Cycle 18
Connection 18
Stream Set Up 19
Authentication 20
Disconnection 20
Summary 20
Chapter 2: Designing XMPP App lications 23
Learning from Others 24
XMPP versus HTTP 27
Advantages of XMPP 28
Disadvantages of XMPP 29
Bridging XMPP and the Web 30
Long Polling 30
Managing the Connection 31
Making JavaScript Speak XMPP 32
Making XMPP Applications 33
The Browser Platform 33
Basic Infrastructure 33
Protocol Design 34
Summary 36
Part II : The App lications
Chapter 3: Saying Hello: The First App lication 39
Application Preview 40
Hello Design 40
Preparing the Way 41
jQuery and jQuery UI 42
Strophe 42
flXHR 43
XMPP Accounts 43
Starting Your First Application 43
User Interface 43
Application Code 45
Making Connections 47
The Connection Life Cycle 47
Creating a Connection 48
Connecting Hello 49
Running the Application 53
Creating Stanzas 53
Strophe Builders 54
Saying Hello 56
Handling Events 57
Adding and Removing Handlers 57
Stanza Matching 57
Stanza Handler Functions 58
Handling Responses in Hello 59
More Hellos 62
Summary 62
Chapter 4: Exp loring the XMPP Protocol:
A Debugg ing Console 63
Application Preview 64
Peek Design 64
Building the Console 65
User Interface 66
Displaying Traffic 68
Making XML Pretty 71
Dealing with XML Input 74
Making Input Easier 76
Exploring XMPP 81
Controlling Presence 81
Probing Versions 81
Dealing with Errors 82
Better Debugging 85
Summary 85
Microblogg ing in Real Time: Chapter 5: An Identica Client 87
Application Preview 88
Arthur Design 88
Microblogging with Identica 89
Creating Your Account 90
Turning on XMPP 90
Building Arthur 90
Getting Started 91
Receiving Messages 93
XHTML-IM 94
Adding XHTML-IM to Arthur 95
Sending Messages 96
Offline Messages 97
Creating a Better Microblogger 101
Summary 102
Chapter 6: Talking with Friends: One‑on‑One Chat 103
Application Preview 104
Gab Design 104
Presence 105
Messages 105
Chat Area 105
Roster Area 106
Making the Interface 106
Building the Roster 109
Requesting Rosters 111
Handling IQs 113
Updating Presence Status 114
Adding New Contacts 116
Responding to Roster Changes 117
Dealing with Subscription Requests 119
Building the Chats 122
Working with Tabs 122
Creating New Chats 123
Sending Messages 124
Best Practices for Instant Messaging 127
Understanding Message Routing 127
Addressing Messages Better 127
Adding Activity Notifications 130
Understanding Chat States 130
Sending Notifications 131
Receiving Notifications 132
Final Touches 133
Gabbing More 143
Summary 143
Exp loring Services: Chapter 7: Service D iscovery
and Browsing 145
Application Preview 146
Dig Design 146
Finding Information 147
Disco#info Queries 147
Disco#items Queries 148
Disco Nodes 149
Creating Dig 149
Initial Disco Queries 153
Browsing the Disco Tree 155
Digging into Services 160
Finding a Proxy Server 161
Discovering Features 162
Looking for a Chat 162
Discovering More 163
Summary 163
Group Chatting: A Multi‑User Chapter 8: Chat Client 165
Application Preview 166
Groupie Design 167
Public Speaking 167
Group Chat Services 167
Entering and Leaving a Room 168
Sending and Receiving Messages 171
Anonymity 171
Creating Rooms 172
Understanding Roles and Affiliations 173
Building the Interface 175
Joining the Room 179
Dealing with Presence and Messages 183
Handling Room Messages 183
Tracking Presence Changes 185
Chat History 186
Keeping It Private 187
Describing Actions 190
Managing the Room 191
Changing Topics 191
Dealing with Troublemakers 192
Recruiting Help 194
Improving Groupie 201
Summary 201
Chapter 9: Publishing and Subscribing:
A Shared Sk etch Pad Introduction 203
SketchCast Preview 204
SketchCast Design 205
Everything Is Pubsub 205
Presenter’s Flow 205
Audience’s Flow 206
Filling Out Forms 206
What Is The Data Forms Extension? 206
Form Elements, Fields, and Types 207
Standardized Form Fields 210
Working with Pubsub Nodes 211
Creating Nodes 211
Configuring Nodes 213
Pubsub Events 215
Publishing to a Node 215
Subscribing and Unsubscribing 216
Retrieving Subscriptions 218
Retrieving Items 219
Subscription Management 221
Broadcasting Sketches Using Pubsub 222
Building the Interface 222
Sketching with Canvas 225
Logging In and Making Nodes 228
Publishing and Receiving Sketch Events 234
Summary 249
Writing Chapter 10: with Friends:
A Collaborative Text Editor 251
Application Preview 252
NetPad Design 252
Operational Transformation 253
Basic Principles 253
Details of the Algorithm 255
Implementation 256
Extending the XMPP Protocol 267
Ignoring the Unknown 267
XML Namespaces 268
Extended Elements 268
Extended Attributes 270
Contributing Extensions 271
Designing the Protocol 271
Testing for Support 272
Requesting and Controlling Sessions 272
Editing Operations 273
Building the Editor 274
The Initial Skeleton 274
Starting Editing Sessions 278
Chatting About Work 284
Making Edits 287
Expanding NetPad 298
Summary 298
laying Games: Chapter 11: Head to Head Tic-T ac-T oe 299
Application Preview 300
Toetem Design 301
Designing the Game Protocol 302
Keeping Track of Users 303
Managing Players 304
Managing Games 305
Playing and Watching the Game 308
Getting Started on Toetem 311
Implementing Sessions and the Waiting List 317
Referee Version One 317
Toetem Client Version One 322
Implementing Game Management 325
Referee Version Two 325
Toetem Client Version Two 334
Implementing the Game Logic 338
The Tic-Tac-Toe Library 338
Referee Version Three 343
Toetem Client Version Three 345
Making the Game More Fun 372
Summary 373
Part III : Advanced Topics
Chapter 12: Getting Attached: Bootstrapp ing BOSH  377
Session Attachment 378
The Mechanics of Sessions 378
Use Cases 379
Automatic Logins with Session Attachment 380
Creating the Django Project 381
Summary 385
Chapter 13: Deploying XMPP App lications 387
Growing Horizontally 387
Multiple Connection Managers 388
Clustering XMPP Servers 391
Spreading Out Components 392
Federating Internally 393
Becoming a Server 393
Growing Vertically 394
Reducing Latency 394
Minimizing XML Serialization 397
Optimizing DOM Operations 398
Summary 399
Writing Chapter 14: Strophe Plug- ins 401
Using Plug-ins 402
Loading Plug-ins 402
Accessing Plug-in Functionality 402
Building Plug-ins 403
Creating a Roster Plug-in 404
Storing Contacts 405
Getting and Maintaining the Roster 407
Manipulating the Roster 411
Taking the Plug-in for a Spin 412
Improving the Roster Plug-in 417
Summary 417
Appendix A: Getting Started with jQuery 419
Appendix B: Setting Up a BOSH Connection Manager 429
Index 441

  ●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●

═════ ═════

Previous Post Next Post