Learn about a simple ZeroMQ application and the basic steps for setting up a client and server in a single process. Also learn about the simplest possible framing and message exchange behavior.
[Instructor] Before properly diving into our chat app, we're going to do a simple warmup exercise. Let's quickly build a server and client which exchange a predefined message. The goal here is to get a feel for ZMQ based applications. It also demonstrates using multiple contexts in a single process to simulate a more complex system. However many details such as socket roles and message framing will not be discussed. As we will review them in depth in later segments. The overall setup is a c sharp console application with a single file, programmed dot cs, which houses all of our code.
As you can see, some of the boilerplate has already been filled in for us, and is not particularly relevant to ZeroMQ. Our main function for instance, on lines 34 through 40, creates a cancellation token source and wires it up to the cancel key press event. This just lets the user interrupt the running program. Then on lines 42 through 46, we launch five separate instances of our client, each as a background task, and each given a unique identifier for debugging purposes.
Finally, on line 49, we launch our server, again as a background task and we wait for it to complete. Let's look at the client code now. Our client which is just a simple function, begins as any ZMQ application would, by creating a new context. With our context in place, we can create a socket. Here we're using a Req socket, but that'll be explained in the next section. After creating our socket, we configure it as needed.
Once our socket is configured, we can go into a loop, where we will send a simple PING message to the server. We will then block waiting to receive a response back from the server. We perform minimal validation just to make sure we're getting approximately the message we expect. And assuming everything is good, we write a small message to the console. Now onto the server.
Our server uses a very similar setup. Again, starting with the creation of a new ZMQ context. Note, that because we have two different contexts, in the same application, it's like having two totally separate applications. They just happen to live in the same process space. We again create a socket from our context, here however, it's a Rep socket. And it is again configured as needed. Once configured, we similarly go into a loop, however, whereas our client will send and then wait to receive, our server will invert that and receive before sending.
Again we have some minimal validation. If everything looks good, we simulate some complex work by sleeping the thread for one second, and we then send a reply to the client. With everything in place, we can press F5 to debug our program. And we should see output similar to the following. And when we're ready to terminate, we can press control c to shut everything down.
- Building a single-file application
- Two-way messaging
- Heartbeating to detect disconnects
- Broadcasting messages using publish/subscribe
- Combining one-way and two-way messaging channels
- Troubleshooting your app
- Designing and evolving protocols