zeromq / chumak

Pure Erlang implementation of ZeroMQ Message Transport Protocol.
Mozilla Public License 2.0
197 stars 47 forks source link

Flushing "old" messages #20

Closed micahrye closed 6 years ago

micahrye commented 6 years ago

This is a question, so you know I am newer to Elixir and this is the first time I have delved into pure Erlang code.

I was wondering if there is a simple way to flush the un-received messages, or get the most recent message, or get the length of queued messages?

A related question I have is if there are any issues with having connection to say a publisher and not checking the messages?

I am running a zmq publisher (in nodejs) and have the following subscriber:

-- Converted my Elixir to Erlang, hopefully no syntax errors --

pub_socket_url = '127.0.0.1'
pub_socket_port = 3400
{ok, pub_socket} = chumak:socket(:sub)
chumak:connect(pub_socket, :tcp, pub_socket_url, pub_socket_port)
chumak:subscribe(pub_socket, <<"node_publisher">>)
chumak:recv(pub_socket)

This works and I get the expected msg from the publisher, but if I do not continue to execute chumak:recv(pub_socket) while the publisher is continuing to publish msgs, then when I do execute chumak:recv(pub_socket) it is an old msg. I would like to be able to get the most recent msg and flush the rest. Is that possible? Or am I suppose to be continuously checking?

Thx

drozzy commented 6 years ago

Hi!

The short answer is yes, you should continuously poll. However, you can put a proxy process in-between you and the polling process, that, say, only keeps the last message. This is erlang/elixir specific functionality. So you might do something like the last value caching example in zmq guide - only you can do it all in erlang/elixir itself: http://zguide.zeromq.org/php:chapter5#Last-Value-Caching

Basically, you should think about what behavior you want exactly, e.g. do you care about missing messages? What about duplicates? Do you always want to get a reply right away or do you want to get "None" if nothing was received recently? After you answer those questions, you can start designing the system around that. It requires a bit of a brain twist.

Good luck, and let me know if this was really confusing!

drozzy commented 6 years ago

P.S.: Feel free to post code in Elixir as well.

micahrye commented 6 years ago

Thanks for answer and code!

drozzy commented 6 years ago

Closing for now. Please feel free to re-open if you have more questions.