www.it-ebooks.info
www.it-ebooks.info
Pieter Hintjens
ZeroMQ
www.it-ebooks.info
ZeroMQ
by Pieter Hintjens
Copyright © 2013 Pieter Hintjens. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (). For more information, contact our corporate/
institutional sales department: 800-998-9938 or
Editors: Andy Oram and Maria Gulick
Production Editor: Christopher Hearse
Copyeditor: Gillian McGarvey
Proofreader: Rachel Head
Indexer: Angela Howard
Cover Designer: Randy Comer
Interior Designer: David Futato
Illustrator: Rebecca Demarest and Kara Ebrahim
March 2013:
First Edition
Revision History for the First Edition:
2013-03-11: First release
See for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. ZeroMQ, the image of a fourhorn sculpin, and related trade dress are trademarks of O’Reilly
Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-33406-2
[LSI]
www.it-ebooks.info
To Noémie, Freeman, and Gregor.
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Part I. Learning to Work with ØMQ
1.
Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Fixing the World 3
Audience for This Book 5
Getting the Examples 5
Ask and Ye Shall Receive 5
A Minor Note on Strings 10
Version Reporting 11
Getting the Message Out 11
Divide and Conquer 16
Programming with ØMQ 21
Getting the Context Right 21
Making a Clean Exit 22
Why We Needed ØMQ 23
Socket Scalability 27
Upgrading from ØMQ v2.2 to ØMQ v3.2 27
Warning: Unstable Paradigms! 28
2.
Sockets and Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
The Socket API 32
Plugging Sockets into the Topology 32
Using Sockets to Carry Data 34
Unicast Transports 35
ØMQ Is Not a Neutral Carrier 35
I/O Threads 36
Messaging Patterns 37
v
www.it-ebooks.info
High-Level Messaging Patterns 38
Working with Messages 39
Handling Multiple Sockets 41
Multipart Messages 44
Intermediaries and Proxies 45
The Dynamic Discovery Problem 45
Shared Queue (DEALER and ROUTER Sockets) 48
ØMQ’s Built-in Proxy Function 53
Transport Bridging 54
Handling Errors and ETERM 56
Handling Interrupt Signals 61
Detecting Memory Leaks 62
Multithreading with ØMQ 63
Signaling Between Threads (PAIR Sockets) 68
Node Coordination 70
Zero-Copy 74
Pub-Sub Message Envelopes 75
High-Water Marks 77
Missing Message Problem Solver 78
3.
Advanced Request-Reply Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Request-Reply Mechanisms 81
The Simple Reply Envelope 82
The Extended Reply Envelope 82
What’s This Good For? 85
Recap of Request-Reply Sockets 85
Request-Reply Combinations 86
The REQ to REP Combination 87
The DEALER to REP Combination 87
The REQ to ROUTER Combination 87
The DEALER to ROUTER Combination 88
The DEALER to DEALER Combination 88
The ROUTER to ROUTER Combination 88
Invalid Combinations 88
Exploring ROUTER Sockets 89
Identities and Addresses 89
ROUTER Error Handling 91
The Load-Balancing Pattern 91
ROUTER Broker and REQ Workers 92
ROUTER Broker and DEALER Workers 94
A Load-Balancing Message Broker 96
A High-Level API for ØMQ 102
vi | Table of Contents
www.it-ebooks.info
Features of a Higher-Level API 104
The CZMQ High-Level API 105
The Asynchronous Client/Server Pattern 111
Worked Example: Inter-Broker Routing 116
Establishing the Details 116
Architecture of a Single Cluster 117
Scaling to Multiple Clusters 118
Federation Versus Peering 121
The Naming Ceremony 122
Prototyping the State Flow 123
Prototyping the Local and Cloud Flows 126
Putting It All Together 133
4. Reliable Request-Reply Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
What Is “Reliability”? 141
Designing Reliability 142
Client-Side Reliability (Lazy Pirate Pattern) 144
Basic Reliable Queuing (Simple Pirate Pattern) 148
Robust Reliable Queuing (Paranoid Pirate Pattern) 151
Heartbeating 159
Shrugging It Off 160
One-Way Heartbeats 160
Ping-Pong Heartbeats 161
Heartbeating for Paranoid Pirate 161
Contracts and Protocols 163
Service-Oriented Reliable Queuing (Majordomo Pattern) 164
Asynchronous Majordomo Pattern 186
Service Discovery 191
Idempotent Services 193
Disconnected Reliability (Titanic Pattern) 194
High-Availability Pair (Binary Star Pattern) 206
Detailed Requirements 208
Preventing Split-Brain Syndrome 211
Binary Star Implementation 211
Binary Star Reactor 218
Brokerless Reliability (Freelance Pattern) 223
Model One: Simple Retry and Failover 225
Model Two: Brutal Shotgun Massacre 228
Model Three: Complex and Nasty 233
Conclusion 244
5. Advanced Publish-Subscribe Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Table of Contents | vii
www.it-ebooks.info
Pros and Cons of Publish-Subscribe 245
Pub-Sub Tracing (Espresso Pattern) 247
Last Value Caching 250
Slow Subscriber Detection (Suicidal Snail Pattern) 254
High-Speed Subscribers (Black Box Pattern) 258
Reliable Publish-Subscribe (Clone Pattern) 260
Centralized Versus Decentralized 261
Representing State as Key-Value Pairs 261
Getting an Out-of-Band Snapshot 271
Republishing Updates from Clients 276
Working with Subtrees 281
Ephemeral Values 284
Using a Reactor 292
Adding the Binary Star Pattern for Reliability 296
The Clustered Hashmap Protocol 306
Building a Multithreaded Stack and API 310
Part II. Software Engineering Using ØMQ
6.
The ØMQ Community. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Architecture of the ØMQ Community 326
How to Make Really Large Architectures 327
Psychology of Software Architecture 328
The Contract 330
The Process 332
Crazy, Beautiful, and Easy 332
Stranger, Meet Stranger 333
Infinite Property 333
Care and Feeding 334
The ØMQ Process: C4 335
Language 335
Goals 336
Preliminaries 338
Licensing and Ownership 339
Patch Requirements 340
Development Process 342
Creating Stable Releases 345
Evolution of Public Contracts 347
A Real-Life Example 349
Git Branches Considered Harmful 352
Simplicity Versus Complexity 353
viii | Table of Contents
www.it-ebooks.info
Change Latency 353
Learning Curve 353
Cost of Failure 353
Up-Front Coordination 354
Scalability 354
Surprise and Expectations 354
Economics of Participation 354
Robustness in Conflict 355
Guarantees of Isolation 355
Visibility 355
Conclusions 355
Designing for Innovation 356
The Tale of Two Bridges 356
How ØMQ Lost Its Road Map 356
Trash-Oriented Design 359
Complexity-Oriented Design 361
Simplicity-Oriented Design 362
Burnout 364
Patterns for Success 366
The Lazy Perfectionist 366
The Benevolent Tyrant 366
The Earth and Sky 366
The Open Door 367
The Laughing Clown 367
The Mindful General 367
The Social Engineer 367
The Constant Gardener 367
The Rolling Stone 368
The Pirate Gang 368
The Flash Mob 368
The Canary Watcher 368
The Hangman 369
The Historian 369
The Provocateur 369
The Mystic 369
7.
Advanced Architecture Using ØMQ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Message-Oriented Pattern for Elastic Design 372
Step 1: Internalize the Semantics 373
Step 2: Draw a Rough Architecture 373
Step 3: Decide on the Contracts 374
Step 4: Write a Minimal End-to-End Solution 374
Table of Contents | ix
www.it-ebooks.info
Step 5: Solve One Problem and Repeat 375
Unprotocols 375
Contracts Are Hard 376
How to Write Unprotocols 377
Why Use the GPLv3 for Public Specifications? 378
Using ABNF 379
The Cheap or Nasty Pattern 380
Serializing Your Data 382
ØMQ Framing 382
Serialization Languages 383
Serialization Libraries 384
Handwritten Binary Serialization 385
Code Generation 386
Transferring Files 392
State Machines 403
Authentication Using SASL 410
Large-Scale File Publishing: FileMQ 411
Why Make FileMQ? 412
Initial Design Cut: The API 412
Initial Design Cut: The Protocol 413
Building and Trying FileMQ 414
Internal Architecture 415
Public API 416
Design Notes 417
Configuration 418
File Stability 419
Delivery Notifications 420
Symbolic Links 420
Recovery and Late Joiners 421
Test Use Case: The Track Tool 423
Getting an Official Port Number 424
8.
A Framework for Distributed Computing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Design for the Real World 426
The Secret Life of WiFi 427
Why Mesh Isn’t Here Yet 428
Some Physics 429
What’s the Current Status? 430
Conclusions 432
Discovery 432
Preemptive Discovery over Raw Sockets 432
Cooperative Discovery Using UDP Broadcasts 434
x | Table of Contents
www.it-ebooks.info
Multiple Nodes on One Device 439
Designing the API 439
More About UDP 448
Spinning Off a Library Project 448
Point-to-Point Messaging 450
UDP Beacon Framing 450
True Peer Connectivity (Harmony Pattern) 452
Detecting Disappearances 454
Group Messaging 455
Testing and Simulation 457
On Assertions 457
On Up-Front Testing 458
The Zyre Tester 459
Test Results 461
Tracing Activity 463
Dealing with Blocked Peers 464
Distributed Logging and Monitoring 467
A Plausible Minimal Implementation 468
Protocol Assertions 470
Binary Logging Protocol 471
Content Distribution 473
Writing the Unprotocol 475
Conclusions 476
9.
Postface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Tales from Out There 479
Rob Gagnon’s Story 479
Tom van Leeuwen’s Story 479
Michael Jakl’s Story 480
Vadim Shalts’s Story 480
How This Book Happened 481
Removing Friction 482
Licensing 484
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Table of Contents | xi
www.it-ebooks.info
www.it-ebooks.info
Preface
ØMQ in a Hundred Words
ØMQ (also known as ZeroMQ, 0MQ, or zmq) looks like an embeddable networking
library, but acts like a concurrency framework. It gives you sockets that carry atomic
messages across various transports, like in-process, inter-process, TCP, and multicast.
You can connect sockets N-to-N with patterns like fan-out, pub-sub, task distribution,
and request-reply. It’s fast enough to be the fabric for clustered products. Its asynchro‐
nous I/O model gives you scalable multicore applications, built as asynchronous
message-processing tasks. It has a score of language APIs and runs on most operating
systems. ØMQ is from iMatix and is LGPLv3 open source.
The Zen of Zero
The Ø in ØMQ is all about trade-offs. On the one hand, this strange name lowers ØMQ’s
visibility on Google and Twitter. On the other hand, it annoys the heck out of some
Danish folk who write us things like “ØMG røtfl”, and “Ø is not a funny-looking zero!”
and “Rødgrød med Fløde!” (which is apparently an insult that means “May your neigh‐
bours be the direct descendants of Grendel!”). Seems like a fair trade.
Originally, the zero in ØMQ was meant to signify “zero broker” and (as close to) “zero
latency” (as possible). Since then, it has come to encompass different goals: zero ad‐
ministration, zero cost, zero waste. More generally, “zero” refers to the culture of min‐
imalism that permeates the project. We add power by removing complexity rather than
by exposing new functionality.
How This Book Came to Be
In the summer of 2010, ØMQ was still a little-known niche library described by its
rather terse reference manual and a living but sparse wiki. Martin Sustrik and I were
sitting in the bar of the Hotel Kyjev in Bratislava plotting how to make ØMQ more
xiii
www.it-ebooks.info
widely popular. Martin had written most of the ØMQ code, and I’d put up the funding
and organized the community. Over some Zlatý Bažant, we agreed that ØMQ needed
a new, simpler website and a basic guide for new users.
Martin collected some ideas for topics to explain. I’d never written a line of ØMQ code
before this, so it became a live learning documentary. As I worked through simple
examples to more complex ones, I tried to answer many of the questions I’d seen on the
mailing list. Because I’d been building large-scale architectures for 30 years, there were
a lot of problems I was keen to throw ØMQ at. Amazingly, the results were mostly simple
and elegant, even when working in C. I felt a pure joy learning ØMQ and using it to
solve real problems, which brought me back to programming after a few years’ pause.
And often, not knowing how it was “supposed” to be done, we improved ØMQ as we
went along.
From the start, I wanted the guide to be a community project, so I put it onto GitHub
and let others contribute with pull requests. This was considered a radical, even vulgar
approach by some. We came to a division of labor: I’d do the writing and make the
original C examples, and others would help fix the text and translate the examples into
other languages.
This worked better than I dared hope. You can now find all the examples in several
languages, and many in a dozen languages. It’s a kind of programming language Rosetta
Stone, and a valuable outcome in itself. We set up a high score: reach 80% translation
and your language gets its own guide. PHP, Python, Lua, and Haxe reached this goal.
People asked for PDFs, and we created those. People asked for ebooks, and got those.
About a hundred people have contributed to the guide to date.
The guide achieved its goal of popularizing ØMQ. The style pleases most and annoys
some, which is how it should be. In December 2010, my work on ØMQ and the guide
stopped, as I found myself going through late-stage cancer, heavy surgery, and six
months of chemotherapy. When I picked up work again in mid-2011, it was to start
using ØMQ in anger for one of the largest use-cases imagineable: on the mobile phones
and tablets of the world’s biggest electronics company.
But the goal of the guide was, from the start, a printed book. So it was exciting to get an
email from Bill Lubanovic in January 2012, introducing me to his editor, Andy Oram,
at O’Reilly, suggesting a ØMQ book. “Of course!” I said. Where do I sign? How much
do I have to pay? Oh, I get money for this? All I have to do is finish it?”
Of course, as soon as O’Reilly announced a ØMQ book, other publishers started sending
out emails to potential authors. You’ll probably see a rash of ØMQ books coming out
next year. That’s good. Our niche library has hit the mainstream and deserves its six
inches of shelf space. My apologies to the other ØMQ authors. We’ve set the bar horribly
high, and my advice is to make your books complementary. Perhaps focus on a specific
language, platform, or pattern.
xiv | Preface
www.it-ebooks.info
This is the magic and power of communities: be the first community in a space, stay
healthy, and you own that space for ever.
Audience
This book is written for professional programmers who want to learn how to make the
massively distributed software that will dominate the future of computing. We assume
you can read C code, because most of the examples here are in C (even though ØMQ
is used in many languages). We assume you care about scale, because ØMQ solves that
problem above all others. We assume you need the best possible results with the least
possible cost, because otherwise you won’t appreciate the trade-offs that ØMQ makes.
Other than that basic background, we try to present all the concepts in networking and
distributed computing you will need to use ØMQ.
Conventions Used in This Book
We used the following typographical conventions in this book:
Italic
Indicates new terms, commands and command-line options, URLs, email address‐
es, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, data types, and environment variables.
Constant width bold
Shows user input at the command line.
Constant width italic
Shows placeholder user input that you should replace with something that makes
sense for you.
This icon signifies a tip, suggestion, or general note.
Using the Code Examples
The code examples are all online in the repository at />tree/master/examples/. You’ll find each example translated into several—often a dozen
—other languages. The examples are licensed under MIT/X11; see the LICENSE file in
that directory. The text of the book explains in each case how to run each example.
Preface | xv
www.it-ebooks.info
We a
ppreciate, but do not require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “ZeroMQ by Pieter Hintjens (O’Reilly).
Copyright 2013 Pieter Hintjens, 978-1-449-33406-2.”
If you feel your use of code examples falls outside fair use or the permission given above,
feel free to contact us at
Safari® Books Online
Safari Books Online (www.safaribooksonline.com) is an on-demand
digital librar
y that delivers expert content in both book and video
form from the world’s leading authors in technology and business.
Technology professionals, software developers, web designers, and business and crea‐
tive professionals use Safari Books Online as their primary resource for research, prob‐
lem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organi‐
zations, government agencies, and individuals. Subscribers have access to thousands of
books, training videos, and prepublication manuscripts in one fully searchable database
from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐
fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John
Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT
Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐
ogy, and dozens more. For more information about Safari Books Online, please visit us
online.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We ha
ve a web page for this book, where we list errata, examples, and any additional
information. You can access this page at />To comment or ask technical questions about this book, send email to bookques
xvi | Preface
www.it-ebooks.info
For more information about our books, courses, conferences, and news, see our website
at .
Find us on Facebook: />Follow us on Twitter: />Watch us on YouTube: />Acknowledgments
Thanks to Andy Oram for making this happen at O’Reilly and editing the book.
Thanks to Bill Desmarais, Brian Dorsey, Daniel Lin, Eric Desgranges, Gonzalo Dieth‐
elm, Guido Goldstein, Hunter Ford, Kamil Shakirov, Martin Sustrik, Mike Castleman,
Naveen Chawla, Nicola Peduzzi, Oliver Smith, Olivier Chamoux, Peter Alexander,
Pierre Rouleau, Randy Dryburgh, John Unwin, Alex Thomas, Mihail Minkov, Jeremy
Avnet, Michael Compton, Kamil Kisiel, Mark Kharitonov, Guillaume Aubert, Ian Bar‐
ber, Mike Sheridan, Faruk Akgul, Oleg Sidorov, Lev Givon, Allister MacLeod, Alexander
D’Archangel, Andreas Hoelzlwimmer, Han Holl, Robert G. Jakabosky, Felipe Cruz,
Marcus McCurdy, Mikhail Kulemin, Dr. Gergö Érdi, Pavel Zhukov, Alexander Else,
Giovanni Ruggiero, Rick “Technoweenie”, Daniel Lundin, Dave Hoover, Simon Jefford,
Benjamin Peterson, Justin Case, Devon Weller, Richard Smith, Alexander Morland,
Wadim Grasza, Michael Jakl, Uwe Dauernheim, Sebastian Nowicki, Simone Deponti,
Aaron Raddon, Dan Colish, Markus Schirp, Benoit Larroque, Jonathan Palardy, Isaiah
Peng, Arkadiusz Orzechowski, Umut Aydin, Matthew Horsfall, Jeremy W. Sherman,
Eric Pugh, Tyler Sellon, John E. Vincent, Pavel Mitin, Min RK, Igor Wiedler, Olof Åkes‐
son, Patrick Lucas, Heow Goodman, Senthil Palanisami, John Gallagher, Tomas Roos,
Stephen McQuay, Erik Allik, Arnaud Cogoluègnes, Rob Gagnon, Dan Williams, Edward
Smith, James Tucker, Kristian Kristensen, Vadim Shalts, Martin Trojer, Tom van Leeu‐
wen, Hiten Pandya, Harm Aarts, Marc Harter, Iskren Ivov Chernev, Jay Han, Sonia
Hamilton, Nathan Stocks, Naveen Palli, and Zed Shaw for their contributions to this
work.
Thanks to Martin Sustrik for his years of incredible work on ZeroMQ.
Thanks to Stathis Sideris for Ditaa.
Preface | xvii
www.it-ebooks.info
www.it-ebooks.info
PART I
Learning to Work with ØMQ
In the first part of this book, you’ll learn how to use ØMQ. We’ll cover the basics, the
API, the different socket types and how they work, reliability, and a host of patterns you
can use in your applications. You’ll get the best results by working through the examples
and text from start to end.
www.it-ebooks.info
www.it-ebooks.info
CHAPTER 1
Basics
Fixing the World
How to explain ØMQ? Some of us start by saying all the wonderful things it does. It’s
sockets on steroids. It’s like mailboxes with routing. It’s fast! Others try to share their
moment of enlightenment, that zap-pow-kaboom satori paradigm-shift moment when
it all became obvious. Things just become simpler. Complexity goes away. It opens the
mind. Others try to explain by comparison. It’s smaller, simpler, but still looks famili‐
ar. Personally, I like to remember why we made ØMQ at all, because that’s most likely
where you, the reader, still are today.
Programming is a science dressed up as art, because most of us don’t understand the
physics of software and it’s rarely, if ever, taught. The physics of software is not algo‐
rithms, data structures, languages, and abstractions. These are just tools we make, use,
and throw away. The real physics of software is the physics of people.
Specifically, it’s about our limitations when it comes to complexity and our desire to
work together to solve large problems in pieces. This is the science of programming:
make building blocks that people can understand and use easily, and people will work
together to solve the very largest problems.
We live in a connected world, and modern software has to navigate this world. So, the
building blocks for tomorrow’s very largest solutions are connected and massively par‐
allel. It’s not enough for code to be “strong and silent” any more. Code has to talk to
code. Code has to be chatty, sociable, and well-connected. Code has to run like the
human brain; trillions of individual neurons firing off messages to each other, a mas‐
sively parallel network with no central control, no single point of failure, yet able to
solve immensely difficult problems. And it’s no accident that the future of code looks
like the human brain, because the endpoints of every network are, at some level, human
brains.
3
www.it-ebooks.info
If you’ve done any work with threads, protocols, or networks, you’ll realize this is pretty
much impossible. It’s a dream. Even connecting a few programs across a few sockets is
plain nasty when you start to handle real-life situations. Trillions? The cost would be
unimaginable. Connecting computers is so difficult that creating software and services
to do this is a multi-billion dollar business.
So we live in a world where the wiring is years ahead of our ability to use it. We had a
software crisis in the 1980s, when leading software engineers like Fred Brooks believed
there was no “silver bullet” to “promise even one order of magnitude of improvement
in productivity, reliability, or simplicity.”
Brooks missed free and open source software, which solved that crisis, enabling us to
share knowledge efficiently. Today we face another software crisis, but it’s one we don’t
talk about much. Only the largest, richest firms can afford to create connected appli‐
cations. There is a cloud, but it’s proprietary. Our data and our knowledge are disap‐
pearing from our personal computers into clouds that we cannot access and with which
we cannot compete. Who owns our social networks? It is like the mainframe-PC rev‐
olution in reverse.
We can leave the political philosophy for another book. The point is that while the
Internet offers the potential of massively connected code, the reality is that this is out
of reach for most of us, and so large, interesting problems (in health, education, eco‐
nomics, transport, and so on) remain unsolved because there is no way to connect the
code, and thus no way to connect the brains that could work together to solve these
problems.
There have been many attempts to solve the challenge of connected software. There are
thousands of IETF specifications, each solving part of the puzzle. For application de‐
velopers, HTTP is perhaps the one solution to have been simple enough to work, but it
arguably makes the problem worse by encouraging developers and architects to think
in terms of big servers and thin, stupid clients.
So today people are still connecting applications using raw UDP and TCP, proprietary
protocols, HTTP, and WebSockets. It remains painful, slow, hard to scale, and essentially
centralized. Distributed peer-to-peer architectures are mostly for play, not work. How
many applications use Skype or BitTorrent to exchange data?
Which brings us back to the science of programming. To fix the world, we needed to
do two things. One, to solve the general problem of “how to connect any code to any
code, anywhere.” Two, to wrap that up in the simplest possible building blocks that
people could understand and use easily.
It sounds ridiculously simple. And maybe it is. That’s kind of the whole point.
4 | Chapter 1: Basics
www.it-ebooks.info
Audience for This Book
We assume you are using the latest 3.2 release of ØMQ. We assume you are using a
Linux box or something similar. We assume you can read C code, more or less, as that’s
the default language for the examples. We assume that when we write constants like
PUSH or SUBSCRIBE, you can imagine they are really called ZMQ_PUSH or ZMQ_SUB
SCRIBE if the programming language needs it.
Getting the Examples
This book’s examples live in the book’s Git repository. The simplest way to get all the
examples is to clone this repository:
git clone depth=1 git://github.com/imatix/zguide.git
Next, browse the examples subdirectory. You’ll find examples by language. If there are
examples missing in a language you use, you’re encouraged to submit a translation. This
is how this book became so useful, thanks to the work of many people. All examples are
licensed under MIT/X11.
Ask and Ye Shall Receive
So let’s start with some code. We’ll begin, of course, with a “Hello World” example. We’ll
make a client and a server. The client sends “Hello” to the server, which replies with
“World” (Figure 1-1). Example 1-1 presents the code for the server in C, which opens
a ØMQ socket on port 5555, reads requests on it, and replies with “World” to each
request.
Example 1-1. Hello World server (hwserver.c)
//
// Hello World server
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
zmq_bind (responder, "tcp://*:5555");
Audience for This Book | 5
www.it-ebooks.info