concept message in category docker

This is an excerpt from Manning's book Bootstrapping Microservices with Docker, Kubernetes and Terraform.
With our storage account created we can now upload our test video. In the Azure portal, with your storage account open, click on Containers in the menu on the left. You should see a message like in figure 4.8 saying you don’t have any containers yet. By the way, just to avoid confusion I need to say that the container we are talking about here is not the same as the containers we are running in our microservices application. A container in Azure Storage is like a directory: it’s a location to store files.
We haven’t yet discussed the styles of messaging we could use, that’s coming soon. For the moment just know that we have multiple techniques we can use to send the viewed message. Through this chapter, we’ll explore our options and we can decide later which one is best suited for this particular situation. Before that though, let’s upgrade our development environment for faster development cycles.
Specifically in this section, we’ll use HTTP POST requests to send messages directly from the video streaming microservice to the history microservice as shown in figure 5.7.
Listing 5.12 is an extract of the index.js from the history microservice that asserts the viewed queue and calls consume to start receiving messages. This causes our function consumeViewedMessage to be called whenever a new message arrives. This is it! There really isn’t very much to receiving simple messages from RabbitMQ.
Listing 5.12 - Consuming “viewed” messages from a RabbitMQ queue (extract from chapter-5/example-3/history/index.js)
// ... code omitted here ... function setupHandlers(app, db, messageChannel) { const videosCollection = db.collection("videos"); function consumeViewedMessage(msg) { #A const parsedMsg = JSON.parse(msg.content.toString()); #B return videosCollection.insertOne({ videoPath: parsedMsg.videoPath }) #C .then(() => { #D messageChannel.ack(msg); #E }); }; return messageChannel.assertQueue("viewed", {}) #F .then(() => { return messageChannel.consume("viewed", consumeViewedMessage); #G }); } // ... code omitted here ...The code in listing 5.12 is only slightly complicated by the fact that we’d like to be sending messages in the JSON format, but RabbitMQ doesn’t natively support JSON, so we must manually parse the incoming message payload.
RabbitMQ is actually agnostic about the format for the message payload and from its point of view, a message is just a blob of binary data. This can be useful in performance-critical cases where we probably want to use a more efficient binary format instead of JSON.

This is an excerpt from Manning's book Learn Docker in a Month of Lunches.
If you see an error from that command, it’s because your setup already has a Docker network called
nat, and you can ignore the message. Now when you run containers you can explicitly connect them to that Docker network using the--networkflag, and any containers on that network can reach each other using the container names.
Asynchronous communication adds a layer between the client and the server. If the client needs a server to do something, it sends a message to the queue. The server is listening on the queue, picks up the message, and processes it. The server can send a response message to the queue, and if the client wants a response, it will be listening on the queue and will pick it up. Asynchronous messaging is like communicating by email--parties can join in when they have free time. If the server is offline or out of capacity, the message sits in the queue until a server is available to pick it up. If the message takes a long time to process, that doesn’t affect the client or the queue. If there’s a failure when the client sends a message, the message isn’t in the queue and the client can safely send it again. Figure 21.1 shows communication with asynchronous messaging.
Run the Redis server in a container, connected to a network where you can run other containers to send and receive messages:
# create the network - on Linux containers: docker network create ch21 # OR Windows containers: docker network create -d nat ch21 # run the Redis server: docker container run -d --name redis --network ch21 diamol/redis # check that the server is listening: docker container logs redis --tail 1Message queues are server components that just run until you stop them. Redis listens for connections on port 6379, and that same address is used by clients to send messages and by servers to listen for messages. You’ll see from your container logs that Redis is up and running just a few seconds after you start the container--my output is in figure 21.2.
Clients need to open a connection to the queue to send their message--and if you’re wondering how that’s better than just calling a REST API directly, it’s all down to speed. Queues usually have their own custom communication protocol, which is highly optimized, so when the client sends a message, it just transmits the bytes of the request and waits for an acknowledgement that it has been received. Queues don’t do any complex processing on the message, so they should easily handle thousands of messages per second.