Home » Categories » Multiple Categories |
How To Work with the ZeroMQ Messaging Library |
Article Number: 270 | Rating: Unrated | Last Updated: Sat, Jan 4, 2014 at 3:18 AM
|
ZeroMQZeroMQ is a library used to implement messaging and communication systems between applications and processes - fast and asynchronously. If you have past experience with other application messaging solutions such as RabbitMQ, it might come a little bit challenging to understand the exact position of ZeroMQ. When compared to some much larger projects, which offer all necessary parts of enterprise messaging, ZeroMQ remains as just a lightweight and fast tool to craft your own. This ArticleAlthough technically not a framework, given its functionality and the key position it has for the tasks it solves, you can consider ZeroMQ to be the backbone for implementing the actual communication layer of your application. In this article, we aim to offer you some examples to inspire you with all the things you can do.
Programming with ZeroMQZeroMQ as a library works through sockets by following certain network communication patterns. It is designed to work asynchronously, and that's where the MQ suffix to its name comes - from thread queuing messages before sending them. ZeroMQ Socket TypesZeroMQ differs in the way its sockets work. Unlike the synchronous way the regular sockets work, ZeroMQ's socket implementation "present an abstraction of an asynchronous message queue". The way these sockets work depend on the type of socket chosen. And flow of messages being sent depend on the chosen patterns, of which there are four:
ZeroMQ Transport TypesZeroMQ offers four different types of transport for communication. These are:
Structuring ZeroMQ ApplicationsZeroMQ works differently than typical and traditional communication set ups. It can have either side of the link (i.e. either the server or the client) bind and wait for connections. Unlike standard sockets, ZeroMQ works by the notion of knowing that a connection might occur and hence, can wait for it perfectly well. Client - Server StructureFor structuring your client and server code, it would be for the best to decide and elect one that is more stable as the binding side and the other(s) as the connecting. Example:
Client - Proxy - Server StructureTo solve the problems caused by both ends of the communication being in a dynamic (hence unstable) state, ZeroMQ provides networking devices (i.e. utensils out of the box). These devices connect to two different ports and route the connections across.
Example:
Programming ExamplesUsing our knowledge from the past section, we will now begin utilizing them to create simple applications. Note:Below examples usually consist of applications running simultaneously. For example, for a client/server setup to work, you will need to have both the client and the server application running together. One of the ways to do this is by using the tool Linux Screen. To install screen on a CentOS system, remember that you can simply run: Simple Messaging Using Request/Reply PatternIn terms of communicating between applications, the request/reply pattern probably forms the absolute classic and gives us a good chance to start with the fundamental basics of ZeroMQ. Use-cases:
Socket type(s) used:
Server Example: server.pyCreate a "server.py" using nano (
When you are done editing, save and e xit by pressing CTRL+X followed with Y. Client Example: client.pyCreate a "client.py" using nano (
When you are done editing, save and exit by pressing CTRL+X followed with Y. Note: When working with ZeroMQ library, remember that each thread used to send a message (i.e. UsageOur Run the server using your Python interpreter:
On another window, send messages using the client application:
Note: To shut down the server, you can use the key combination: Ctrl+C Working with Publish/Subscribe PatternIn the case of publish/subscribe pattern, ZeroMQ is used to establish one or more subscribers, connecting to one or more publishers and receiving continuously what publisher sends (or seeds). A choice to specify a prefix to accept only such messages beginning with it is available with this pattern. Use-cases: Publish/subscribe pattern is used for evenly distributing messages across various consumers. Automatic updates for scoreboards and news can be considered as possible areas to use this solution. Socket type(s) used:
Publisher Example: pub.pyCreate a "pub.py" using nano (
When you are done editing, save and exit by pressing CTRL+X followed with Y. Subscriber Example: sub.pyCreate a "sub.py" using nano (
When you are done editing, save and exit by pressing CTRL+X followed with Y. Note: Using the UsageOur Run the publisher to send messages:
On another window, see the print outs of subscribed content (i.e.
Note: To shut down the subscriber and the publisher applications, you can use the key combination: Ctrl+C Pipelining the Pub./Sub. with Pipeline Patter (Push/Pull)Very similar in the way it looks to the Publish/Subscribe pattern, the third in line Pipeline pattern comes as a solution to a different kind of problem: distributing messages upon demand. Use-cases: Pipelining pattern can be used in cases where are list of queued items need to be routed (i.e. pushed in line) for the one asking for it (i.e. those who pull). Socket type(s) used:
PUSH Example: manager.pyCreate a "manager.py" using nano (
The file PULL Example: worker_1.pyCreate a "worker_1.py" using nano ( import zmq
The file UsageOur Run the publisher to send messages:
On another window, see the print outs of subscribed content (i.e.
Note: To shut down the subscriber and the publisher applications, you can use the key combination: Ctrl+C Exclusive Pair PatternExclusive pair pattern implies and allows establishing one-tone sort of communication channels using the Bind Example: bind.pyCreate a "bind.py" using nano (
When you are done editing, save and exit by pressing CTRL+X followed with Y. Connect Example: connect.pyCreate a "connect.py" using nano (
When you are done editing, save and exit by pressing CTRL+X followed with Y. UsageYou can use the above example to create any bidirectional uni-connection communication applications. Note: To shut down either, you can use the key combination: Ctrl+C |
Attachments
There are no attachments for this article.
|
How To Create Nagios Plugins With Bash On CentOS 6
Viewed 3469 times since Sat, Jan 4, 2014
How To Set Up ProFTPD on CentOS 6
Viewed 3041 times since Thu, Jan 2, 2014
How To Install Linux, Apache, MySQL, PHP (LAMP) stack On CentOS 6
Viewed 6511 times since Thu, Dec 26, 2013
How To Install Ruby on Rails on CentOS 6
Viewed 3053 times since Tue, Dec 31, 2013
How to Setup Additional Entropy for Cloud Servers Using Haveged
Viewed 2524 times since Sat, Jan 4, 2014
How To Create Nagios Plugins With PHP On CentOS 6
Viewed 7074 times since Sat, Jan 4, 2014
How to Setup and Configure an OpenVPN Server on CentOS 6
Viewed 3048 times since Tue, Dec 31, 2013
Common Python Tools: Using virtualenv, Installing with Pip, and Managing
Viewed 2140 times since Sat, Jan 4, 2014
How To Install Wordpress with nginx on CentOS 6
Viewed 9208 times since Sat, Jan 4, 2014
Docker Explained: How To Containerize Python Web Applications
Viewed 4614 times since Sat, Jan 4, 2014
|