langohr icon indicating copy to clipboard operation
langohr copied to clipboard

Consider providing serialization-aware consumer API

Open michaelklishin opened this issue 12 years ago • 11 comments

Many scenarios in message consumer applications are very common. For example, JSON or MsgPack used for serialization. We should investigate a convenient way to plug common serialization formats into consumer API. Then payload attribute can be a data structure instead of a bag of bytes.

michaelklishin avatar Apr 19 '12 13:04 michaelklishin

yes, and clojure data structure (de)serialization:

(defn serialize [data]
  (binding [*print-dup* true] (pr-str data)))

(defn deserialize [str]
  (with-in-str str (read))

suggested content-type: application/clojure

carlhoerberg avatar Apr 19 '12 13:04 carlhoerberg

https://github.com/mefesto/wabbitmq has generic serialization support, could be worth checking out..

carlhoerberg avatar Apr 19 '12 13:04 carlhoerberg

We probably can take a page from Welle which in turn steals this idea from Sumo. I don't want this to replace langohr.consumers/subscribe, though, just be an useful option to choose from.

michaelklishin avatar Apr 19 '12 13:04 michaelklishin

maybe even include content-encoding, eg. automatic gzip (de)compression of payloads..

carlhoerberg avatar Mar 19 '13 11:03 carlhoerberg

I've written my own abstraction on top that uses a ring style to attach additional behaviors like this. So I have a wrap-decode-payload wrapper that support JSON or EDN based on content-type. Similarly, I have a wrap-encode-payload for going the other way and several other wrappers for other purposes. Gzip could be supported the same way. This pattern seems to work pretty well.

mlimotte avatar Dec 13 '13 23:12 mlimotte

@mlimotte yes, I was contemplating doing just that.

michaelklishin avatar Dec 14 '13 08:12 michaelklishin

Has the release of transit any incidence over this discussion?

danielsz avatar Aug 30 '14 03:08 danielsz

Transit doesn't change anything for us. We can't support a single transport only.

michaelklishin avatar Aug 30 '14 11:08 michaelklishin

OK. Thanks.

Just to be clear: is it up to everyone to build their abstractions on top of langohr for serialization/deserialization, or are there features/best practices/examples that can help when starting out?

danielsz avatar Aug 30 '14 15:08 danielsz

Well, if you can deserialize a byte array in your consumer, you are all set today.

We can provide a protocol and a deserializing consumer function that will do it for you and call your handler with a Clojure data structure, that's pretty much it.

michaelklishin avatar Aug 30 '14 16:08 michaelklishin

Thanks, Michael, I'll take the first suggestion for now.

Coming from Ruby, I'm always surprised when there's no native data structures straight off the box. Not a big deal, but it's a philosophy thing that puzzles me.

danielsz avatar Aug 30 '14 18:08 danielsz