I’ve been working on a RabbitMQ publisher and discovered that you can only publish strings to a RabbitMQ queue. The problem is I’m trying to publish a map to the RabbitMQ queue and it won’t let me.
For example:
my_map = %{message: "test", userid: 234}
I tried using Jason.encode!(my_map)
to serialize the map before publishing it to the queue, which seems to work. But when I receive the message out of the queue in a later part of the app, I use Jason.decode!(my_map_from_queue)
, and I get this:
my_map = %{"message" => "test", "userid" => 234}
I also considered:
:erlang.term_to_binary(my_map)
:erlang.binary_to_term(my_map)
However, this thread mentioned it is unsafe to do on untrusted data: Serialize and unserialize structs, tuples, keyword lists, atoms? - #5 by sezaru
My intention is to save messages from a chat room to the database, so the message text being submitted could be anything and untrusted input coming from the chat room.
The original map being submitted is pre-formatted to a specific database schema. The goal was to gather all chat messages to RabbitMQ and use a producer to collect these messages in batches and use Repo.insert_all([list of maps]). The hope was that anything going into RabbitMQ would come out exactly the same.
What is a good solution for the serialization or “stringifying” and “destringifying” things going into and out of RabbitMQ queues?