Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any recommended lightweight pubsub service/library? [closed]

I'm building a small system that contains many parts and I want to use a message pub/sub service to communicate between parts.

I read about some message queue services like RabbitMQ and ZeroMQ but I feel they are too complicated and feel like it was born for distributed system. All parts of my system will be written in C++/Linux and place on a small Raspberry Pi CPU, so I dont need feature like scalable, cross-platform, other language clients ...

Can you guys give me some advice about services or libraries that fit my need?

like image 653
Yoshi Avatar asked Jan 22 '13 08:01

Yoshi


People also ask

Is Pubsub scalable?

The system is designed to be horizontally scalable, where an increase in the number of topics, subscriptions, or messages can be handled by increasing the number of instances of running servers.

Which protocol is working on pub/sub model?

Some other popular protocols like the Message Queue Telemetry Transport ( MQTT ) also follows the Pub/Sub pattern. MQTT is an ISO protocol for messaging between IoT devices.

Why do we need Pubsub?

Publish/Subscribe (Pub/Sub) messaging provides instant event notifications for these distributed applications. The Publish Subscribe model enables event-driven architectures and asynchronous parallel processing, while improving performance, reliability and scalability.

What is difference between request reply and publish subscribe?

A request reply messaging model differs from a traditional pub/sub or P2P model, where a message is published to a Topic or Queue and a client can consume the message without providing a reply message in response to the publisher.


1 Answers

It's not that hard to do yourself actually.

First of all you need to define the protocol to be used. It can be very simple; like just a message type field, a payload size field, and the actual payload. The message types you need SUBSCRIBE, UNSUBSCRIBE and PUBLISH. The payload for the SUBSCRIBE and UNSUBSCRIBE messages is the name of a channel to subscribe to/unsubscribe from. The payload for the PUBLISH message is the channel name and the actual data (together with the size of the data of course).

To connect all subscribers you need a central server. All subscribers/publishers needs to connect to this server. The server program keeps a collection of queues, one for each channel. When a subscribe or publish message arrives to the server for a channel that doesn't exist, create a new message queue for this channel. For each channel the server also needs a collection of all clients subscribes to that channel. When a publish message arrives to the server, it's added to the end of the queue for the channel in question. While a channel queue is not empty, send a copy of it to all subscribers for that channel, and when all have received it then the message can be removed from the queue.

The hard part of the server is probably going to be the communication part. The easy part will be all queues and collections, as you can use the C++ standard containers for all of them (e.g. std::queue for the actual queue, std::unordered_map for channels, and std::vector for the collection of connected clients.)

The clients are very simple, all the need to do is being able to send the subscription and publish messages to the server, and receive the publish messages from the server. The hard part will once again be the actual communication part.


Postscript:

I've never actually built such a system my self, all of the above was just directly of the top of my head. An experienced programmer shouldn't need more than a couple of hours to implement the basics, maybe a couple of days for an inexperienced one.

For the communication you could use e.g. Boost ASIO, maybe use one threads per channel. And you can use something like Boost property tree to construct/parse JSON or XML messages.

However, all of this is kind of reinventing the wheel, when you could probably start using one of the existing systems like RabbitMQ in a couple of hours, saving you a lot of time (and a lot of bugs!)

like image 111
Some programmer dude Avatar answered Oct 20 '22 04:10

Some programmer dude