1
votes

There is a server, that collects some data and then sends it to the client. I need to be sure that the client got the data sent by server. I thought PUB/SUB pattern here would be best choice, but I don't understand how to make check if client got the data? I heard something about reliable PUB/SUB, but there's no real example.

Any thought, suggestions, examples and help?

Simple publisher:

import zmq

context = zmq.Context()

server_socket = context.socket(zmq.PUB)
server_socket.bind('tcp://*:5559')

while True:
    server_socket.send('message')

Simple subscriber:

import zmq

context = zmq.Context()

client_socket = context.socket(zmq.SUB)
client_socket.setsockopt(zmq.SUBSCRIBE, '')
client_socket.connect('tcp://localhost:5559')

while True:
    print client_socket.recv()

In this example i need to be shure that subscriber got 'message'.

2
Im probably going to show my ignorance of networking here, but isnt TCP a reliable communication protocol inherently, and the only way a subscriber wouldnt get the message is if they are not connected? - Joran Beasley
It is true, but i need to check if something is crashed and in that case i need to resend message. F.e. client crashes for 20 sec, but server still is sending messages and when client wake up, a bunch of messages will be lost. - Dmitrijs Zubriks
could you not just open a second port and listen to that for confirmations? - Joran Beasley

2 Answers

3
votes

In its general form, pubsub is not appropriate for your use case. One of the basic principles of pubsub is that publishers and subscribers are decoupled. I.e. the publisher shouldn't be aware of its subscribers, and shouldn't be affected by them. There may be any number of subscribers, including none.

You seem to be requiring a single subscriber, which breaks that.

0
votes

The ZMQ guide talks about how the first few messages from a PUB socket will always be dropped, and how if you want the reliability you're talking about, you need the SUB end to send back a confirmation that it received each message.

You may want to consider using a PUSH/PULL connection instead. Replace your PUB with a PUSH and your SUB with a PULL. Maybe bind the PUSH end. See: ZMQ PUSH/PULL