tanszek:oktatas:iss_t:messaging_systems
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tanszek:oktatas:iss_t:messaging_systems [2023/04/24 19:04] – [MQTT example] knehez | tanszek:oktatas:iss_t:messaging_systems [2025/04/14 07:31] (current) – [MQTT example] knehez | ||
---|---|---|---|
Line 15: | Line 15: | ||
A message queue is a software that enables communication between different software components in a distributed system. It allows components to exchange messages asynchronously, | A message queue is a software that enables communication between different software components in a distributed system. It allows components to exchange messages asynchronously, | ||
- | RabbitMQ (https:// | + | RabbitMQ (https:// |
In RabbitMQ, messages are published by producers to a specific exchange, which routes them to one or more queues based on the specified routing key. Consumers then subscribe to the queues and receive messages. RabbitMQ supports multiple programming languages, including Java, Python, .NET, and Node.js, making it a versatile messaging solution for various use cases. | In RabbitMQ, messages are published by producers to a specific exchange, which routes them to one or more queues based on the specified routing key. Consumers then subscribe to the queues and receive messages. RabbitMQ supports multiple programming languages, including Java, Python, .NET, and Node.js, making it a versatile messaging solution for various use cases. | ||
Line 53: | Line 53: | ||
This code sets up a callback function that will be called every time a message is received from the ' | This code sets up a callback function that will be called every time a message is received from the ' | ||
- | ==== Type of " | + | ===== |
An exchange in RabbitMQ is a messaging entity that receives messages from producers and routes them to queues based on some criteria. When a producer sends a message to RabbitMQ, it sends the message to an exchange. The exchange then examines the message' | An exchange in RabbitMQ is a messaging entity that receives messages from producers and routes them to queues based on some criteria. When a producer sends a message to RabbitMQ, it sends the message to an exchange. The exchange then examines the message' | ||
Line 107: | Line 107: | ||
Each exchange type has its own routing algorithm and is used in different messaging scenarios. Understanding the exchange types is important when designing RabbitMQ architectures that meet specific business requirements. | Each exchange type has its own routing algorithm and is used in different messaging scenarios. Understanding the exchange types is important when designing RabbitMQ architectures that meet specific business requirements. | ||
- | ==== MQTT example ==== | + | ===== MQTT example |
Clone repository into docker playground: | Clone repository into docker playground: | ||
git clone https:// | git clone https:// | ||
+ | cd isi/ | ||
+ | docker-compose up | ||
docker-compose.yml defines a multi-container application with three services: mqtt, consumer, and producer. | docker-compose.yml defines a multi-container application with three services: mqtt, consumer, and producer. | ||
Line 184: | Line 186: | ||
* CMD [" | * CMD [" | ||
+ | **consumer.py** | ||
+ | <code python> | ||
+ | import paho.mqtt.client as mqtt | ||
+ | |||
+ | broker = " | ||
+ | port = 1883 | ||
+ | |||
+ | timelive = 60 | ||
+ | |||
+ | def on_connect(client, | ||
+ | print(" | ||
+ | client.subscribe("/ | ||
+ | |||
+ | |||
+ | def on_message(client, | ||
+ | print(msg.payload.decode()) | ||
+ | |||
+ | client = mqtt.Client() | ||
+ | client.connect(broker, | ||
+ | client.on_connect = on_connect | ||
+ | client.on_message = on_message | ||
+ | client.loop_forever() | ||
+ | </ | ||
+ | |||
+ | **producer.py** | ||
+ | |||
+ | <code python> | ||
+ | # simulator device 1 for mqtt message publishing | ||
+ | import paho.mqtt.client as paho | ||
+ | import time | ||
+ | import random | ||
+ | |||
+ | broker = " | ||
+ | port = 1883 | ||
+ | |||
+ | def on_publish(client, | ||
+ | print(" | ||
+ | |||
+ | client = paho.Client(" | ||
+ | client.on_publish = on_publish | ||
+ | client.connect(broker, | ||
+ | |||
+ | for i in range(20): | ||
+ | d = random.randint(1, | ||
+ | |||
+ | # telemetry to send | ||
+ | message = " | ||
+ | |||
+ | time.sleep(d) | ||
+ | |||
+ | # publish message | ||
+ | ret = client.publish("/ | ||
+ | |||
+ | print(" | ||
+ | </ | ||
tanszek/oktatas/iss_t/messaging_systems.1682363087.txt.gz · Last modified: 2023/04/24 19:04 by knehez