Table of Contents
ØMQ - The Guide
By Pieter Hintjens, CEO of iMatix
Please use the issue tracker for all comments and errata. This version covers the latest stable release of ØMQ
(3.2). If you are using older versions of ØMQ then some of the examples and explanations won't be accurate.
The Guide is originally in C, but also in PHP, Python, Lua, and Haxe. We've also translated most of the
examples into C++, C#, CL, Delphi, Erlang, F#, Felix, Haskell, Java, Objective-C, Ruby, Ada, Basic, Clojure,
Go, Haxe, Node.js, ooc, Perl, and Scala.
Preface
top prev next
ØMQ in a Hundred Words
top prev next
Ø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 asynchronous
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.
How It Began
top prev next
Do you need professional PDFs? Try PDFmyURL!
We took a normal TCP socket, injected it with a mix of radioactive isotopes stolen from a secret Soviet atomic
research project, bombarded it with 1950-era cosmic rays, and put it into the hands of a drug-addled comic
book author with a badly-disguised fetish for bulging muscles clad in spandex. Yes, ØMQ sockets are the
world-saving superheroes of the networking world.
Figure 1 - A terrible accident…
The Zen of Zero
top prev next
The Ø in ØMQ is all about tradeoffs. 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 neighbours be the direct descendants of Grendel!" Seems like a fair trade.
Originally the zero in ØMQ was meant as "zero broker" and (as close to) "zero latency" (as possible). Since
then, it has come to encompass different goals: zero administration, zero cost, zero waste. More generally,
"zero" refers to the culture of minimalism that permeates the project. We add power by removing complexity
rather than by exposing new functionality.
Audience
top prev next
Do you need professional PDFs? Try PDFmyURL!
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.
Acknowledgements
top prev next
Thanks to Andy Oram for making the O'Reilly book happen, and editing this text.
Thanks to Bill Desmarais, Brian Dorsey, Daniel Lin, Eric Desgranges, Gonzalo Diethelm, 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 Barber, 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 Åkesson, 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 Leeuwen, 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.
Chapter 1 - Basics
top prev next
Fixing the World
top prev next
How to explain ØMQ? Some of us start by saying all the wonderful things it does. It's sockets on steroids. It's
Do you need professional PDFs? Try PDFmyURL!
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 familiar.
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 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 algorithms, data structures, languages and abstractions.
These are just tools we make, use, throw away. The real physics of software is the physics of people—
specifically, 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 parallel. 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, well-connected. Code has to
run like the human brain, trillions of individual neurons firing off messages to each other, a massively 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.
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 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 applications. There is a cloud, but it's proprietary. Our data and
our knowledge is disappearing 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 revolution 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, economics, 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 code. There are thousands of IETF
specifications, each solving part of the puzzle. For application developers, 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
Do you need professional PDFs? Try PDFmyURL!
Websockets. It remains painful, slow, hard to scale, and essentially centralized. Distributed P2P 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.
Starting Assumptions
top prev next
We assume you are using at least version 3.2 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_SUBSCRIBE if the programming language needs it.
Getting the Examples
top prev next
The examples live in a public GitHub 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 text became so useful, thanks to
the work of many people. All examples are licensed under MIT/X11.
Ask and Ye Shall Receive
top prev next
So let's start with some code. We start 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". Here's the server in C, which opens
a ØMQ socket on port 5555, reads requests on it, and replies with "World" to each request:
// Hello World server
Do you need professional PDFs? Try PDFmyURL!
评论0