mutalk
C# UDP multicast message library inspired by this repo.
main idea
In the world of IoT we often face with the problem how to organize communication between peers.
We often need a protocol that:
- Can work without dedicated broker.
- Reliable but rare messages losses are acceptable
- Based on a pub/sub design.
- Requires minimum getting started efforts
It looks like udp multicast messaging is an ideal candidate for such kind of protocol.
the essence of the library
- This is c# micro-library that implements pub/sub udp multicast messaging mechanism
- Each message is a valid json based string
- Each message consists of "topic" and "body" parts. Example
{"t":"test123", "m":"SGVsbG8gd29ybGQgIQ=="}
- Body of message is base64 encoded string (so you may send an arbitrary bytes)
- Sender/publisher need to know only about topic to send messages.
- Receiver/subscriber need to know only about topic for consuming messages.
- Library hashes "topics" and maps them to ip multicast range "224.0.0.1-224.255.255.254"
from sender/publisher perspective
using var mutalk = new Mutalk("test123"); // "test123" is topic name where messages will be sent
mutalk.SendMessage(Encoding.UTF8.GetBytes("Hello")); // send first message to topic
Task.Delay(1000).Wait();
mutalk.SendMessage(Encoding.UTF8.GetBytes("World !")); // send second message to topic
from receiver/subscriber perspective
using var mutalk = new Mutalk("test123");
mutalk.OnMessage += (_, eventArgs) =>
{
if (eventArgs.Topic == "test123") // double check topic name due to possible hash collisions
{
Console.WriteLine(Encoding.UTF8.GetString(eventArgs.Message));
}
};
mutalk.ReceiveMessages(cancellationToken); // this is a blocking call until cancellationToken.Cancel()