Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid to use PULL/PUSH with ROUTER socket? #81

Closed
raffian opened this issue Aug 17, 2013 · 5 comments
Closed

Valid to use PULL/PUSH with ROUTER socket? #81

raffian opened this issue Aug 17, 2013 · 5 comments

Comments

@raffian
Copy link

raffian commented Aug 17, 2013

Is PUSH/PULL valid to use with ROUTER? There's no mention of such a socket combo in the guide, I found no examples either.

I'm building a chat/im program, the server uses ROUTER/ROUTER, the clients use PUSH/PULL for sending and receiving messages from server. I also use client identities so the server can route messages to specific clients (for private chat, etc). It appears to work fine in my testing. I just want to know if this is a valid approach.

@miniway
Copy link
Member

miniway commented Aug 18, 2013

PUSH/PULL is valid to use with ROUTER. But I would recommend to use DEALER as a counter part of ROUTER.

By using a single DEALER at the client side, you can send and receive message to/from server. It might be easier to implement.

@raffian
Copy link
Author

raffian commented Aug 18, 2013

Hi Dongmin,

My client uses 2 threads; one for PUSHing messages, and another for PULLing; works great since everything on the client is async. If I switch to a single DEALER, I still need two threads on the client, and the guide says to never reuse sockets across threads. Can I break that rule if one thread always receives while the other thread always sends on the same DEALER socket? Otherwise, I don't see how this can be accomplished, but I'm open to suggestions.

@miniway
Copy link
Member

miniway commented Aug 19, 2013

I like a pattern using an agent. A sending thread sends messages to the agent through a pipe(INPROC socket), which ZThread.fork do a magic for you. You can use a separate receiving thread and pipe or utilize the sending thread for receiving.

A sudo code of would be

// Agent
senderPipe = ZThread.fork(ctx, sender)
receiverPipe = ZThread.fork(ctx, receiver)
sock = ctx.createSocket(ZMQ.DEALTER).connect(endpoint)

PollItem[] items = {
                    new PollItem(senderPipe, ZMQ.Poller.POLLIN),
                    new PollItem(sock, ZMQ.Poller.POLLIN)
            };
while (true) {
                int rc = ZMQ.poll(items, timeout)
                if (rc == -1)
                    break;              //  Context has been shut down

                if (items[0].isReadable())   // sender sent a message
                    sock.send(senderPipe.recv())

                if (items[1].isReadable())  // receive a message from outside
                    receiverPipe.send(sock.recv())

                // do your heartbeat 
}

Please refere chapter4 of the guide and https://github.com/imatix/zguide/blob/master/examples/Java/flcliapi.java for the agent pattern, or zyre https://github.com/zeromq/zyre/tree/master/java

@raffian
Copy link
Author

raffian commented Aug 19, 2013

Very nice, and I just got this code from irc, so from these examples I am in good shape, thank you
https://gist.github.com/minrk/6265022

@raffian raffian closed this as completed Aug 19, 2013
@crystaljuicelabs
Copy link

Hi Raffian...were you able to get it running fully?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants