In C we have Sockets and descriptors, it is possible to just take one of these and hand them over to a Thread, this makes it possible that you can accept incoming connections and give the work to a Thread you like and the Thread can send by itself the response back.
My question is, how can I achieve this with ZeroMQ? With Request-Reply pattern it seems i cannot send and receive asynchronously, the responses have to be in sequence, my goal would be to have multiple clients to a single server, sending responses not in sequence.
I had a look at the Request Response pattern but the API clearly states that using that Socket with multiple Threads is a bad idea. Maybe i have missed something or ZeroMQ is more intelligent than i know of. If you need any further information just post a comment and i will do my best to give the information.
I had also a look at the provided examples: Code Examples
Here is the Socket description: ZMQ-Socket
ØMQ sockets are not threadsafe. Technically it's possible to do this, but it demands semaphores, locks, or mutexes. This will make your application slow and fragile.
ZeroMQ is an open-source, high-performance messaging library. It is cross-platform and supports cross languages, and is light-weight and fast.
ZeroMQ patterns are implemented by pairs of sockets with matching types. The built-in core ZeroMQ patterns are: Request-reply, which connects a set of clients to a set of services. This is a remote procedure call and task distribution pattern.
ZeroMQ supports common messaging patterns (pub/sub, request/reply, client/server and others) over a variety of transports (TCP, in-process, inter-process, multicast, WebSocket and more), making inter-process messaging as simple as inter-thread messaging. This keeps your code clear, modular and extremely easy to scale.
Often, when you try to adapt existing designs to 0MQ, you get into trouble. Here the existing design passes sockets (typically, HTTP) to child processes or threads, and then allows them to respond. It's not a very elegant design. It works because each new connection exists as a socket, and because when a reply is sent the socket is destroyed. Neither of these apply to 0MQ applications.
The 0MQ design uses a frontend ROUTER socket that pulls in requests from clients. It then passes these requests to worker threads across inproc:// sockets, using a backend DEALER to deal out the requests. The worker threads use a REP socket to receive the requests, and send their replies back on. The main thread then polls on both frontend and backend socket, simply routing messages between the two.
Which is what that old blog article explains (using the legacy XREP/XREQ names), and it's explained in more detail with examples in many languages here: http://zguide.zeromq.org/page:all#Multithreading-with-MQ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With