I made a service which accepts requests on a 0MQ router socket in NetMQ, and acts mostly like a cache for other apps, i.e. it can receive a "GET" request and send a response, or it can receive a "PUT" request which doesn't need a response.
I have two issues though:
I tried using a
RequestSocket
in client code, but it still fails if I don't wait for a response after a sent message (I guess this is how REQ/REP works in 0MQ). What is the appropriate 0MQ socket type if I only want to get a response for some message types?Currently my client code creates a new
RequestSocket
instance whenever it needs to communicate with the service. Is there a thread-safe way in NetMQ where I could have a single socket that can be reused over the client code when I need to send a request and get a response, without the need to create a new connection each time?
The service code is currently basically:
var socket = new RouterSocket(endpoint);
socket.ReceiveReady += (sender, e) =>
{
var msg = e.Socket.ReceiveMultipartMessage();
var action = msg[3].ConvertToString();
switch (action)
{
case "GET":
{
var key = msg[4].ConvertToString();
var data = _cache.Get(key);
var response = new NetMQMessage();
response.Append(msg[0]); // msg id
response.Append(NetMQFrame.Empty);
response.Append(msg[4]);
response.Append(data);
e.Socket.SendMultipartMessage(response);
}
break;
case "SET":
{
var key = msg[4].ConvertToString();
var data = msg[5].Buffer;
_cache.Set(key, data);
// no response needed here, but needed for RequestSocket clients
}
break;
default: throw new NotImplementedException();
}
};