Skip to content

yuridiniz/xMQ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 

Repository files navigation

eXtendable Message Queue

Provides a message queue with the purpose of being simple, performative and effective, and can be easily extended

Draft

This project is not yet recommended for use in a production environment

Summary

Getting Started:

Create Server:

var socket = new PairSocket("tcp://127.0.0.1:5001"); // Server Host Address
socket.OnMessage += (remote, msg) => { };
socket.OnConnected += (remote) => { };
socket.OnDisconnected += (remote) => { };

socket.Bind(); //Call Bind()

Console.ReadKey();

Create Client:

var socket = new PairSocket("tcp://127.0.0.1:5001"); // Server address
socket.Id = "my client id :)";
socket.OnMessage += (remote, msg) => { };
socket.OnConnected += (remote) => { };
socket.OnDisconnected += (remote) => { };

socket.Connect(); //Call Connect()

Console.ReadKey();

Send and Receive mensage

Asynchronous

Send:

/* ... server or client connection setup ... */

var msg = new Message();
msg.Append(0x1); // My command, but it can be a string, a Guid or another primite type :)
msg.Append("Hello");
msg.Append("World");

// If this call is being made on the server, you will have to obtain your client's socket
// Através do método socket.GetClients() ou socket.GetClient("my client id :)")

var success = socket.Send(msg);

Listerner:

/* ... server or client connection setup ... */
socket.OnMessage += (remote, msg) => { 
   var myCommand = msg.ReadNext<int>();
   if(myCommand == 0x1) 
   {
      var firstToken = msg.ReadNext<string>();
      var secondToken = msg.ReadNext<string>();
      
      Console.WriteLine(firstToken);
      Console.WriteLine(secondToken);
      
      // If it is necessary to send a new message, 
      // we recommend that you write in the same message received, 
      // so that if the message contains a Reply Id, the server will be able to find the requester
      
      msg.Append(0x5); // My own response command if needed for your protocol
      msg.Append("Thanks");
      
      remote.Send(msg);
   }
}

Request/Reply

Request:

/* ... server or client connection setup ... */

var msg = new Message();
msg.Append(0x1); // My command, but it can be a string, a Guid or another primite type :)
msg.Append("Hello");
msg.Append("World");

int timeout = 500; // -1 for infinty timeout
var response = socket.Request(msg, timeout);
if(response.Success) {
  var firstToken = response.ReadNext<int>(); //What will the server command be? Did he send a 0x5 or 0x6 to me? :)
  var secondToken = response.ReadNext<string>();
  Console.WriteLine(secondToken);
}

Reply:

/* ... server or client connection setup ... */
socket.OnMessage += (remote, msg) => { 
   var myCommand = msg.ReadNext<int>();
   if(myCommand == 0x1) 
   {
      var firstToken = msg.ReadNext<string>();
      var secondToken = msg.ReadNext<string>();
      
      Console.WriteLine(firstToken);
      Console.WriteLine(secondToken);
      
      // If it is necessary to send a new message, 
      // we recommend that you write in the same message received, 
      // so that if the message contains a Reply Id, the server will be able to find the requester
      
      msg.Append(0x5); // My own response command if needed for your protocol
      msg.Append("Thanks");
      
      remote.Send(msg);
   }
}

Motivation

For some time I admire the communication in distributed network protocols. The problems of distributed computing have always appealed to me a lot. Over time, I experimented with many MQ protocols, among them RabbitMQ, Mqtt, Kafka, Socket.io, SignalR and they are all really very incredible and very powerful

However, they all had something that bothered me, it was always a bazooka to kill an ant, and sometimes I needed to make this bazooka work like a gun that was what I needed

Inspiration

That was when I met ZeroMQ, which proposed to do everything I needed, a simple protocol so that you could implement my own protocol on it. An incredible project, with an approach without Broker, but I soon saw some limitations.

The project makes an abstraction that masks connection errors, and that for me would already make its use unfeasible. To get around this, you should implement a pooling on it to check the active connections. Other limitations also made me discouraged, such as the fact that you need a Socket object for each type of communication. So, if you want to work with Req/Resp and Pub/Sub, you should use 3 Sockets type, one for Req/Resp, one for Producer and one for Subscriber

About

A simple and little message queue protocol

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages