peppy
How to serialize/unserialize elixir map data for RabbitMQ?
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?
Most Liked
SirWerto
I think you can use binary_to_term/2 with the safe option.
:erlang.binary_to_term(my_map, [:safe]).
this only use atoms that have been created earlier, but you have to deal with the possible exception raising.
dimitarvp
benwilson512
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









