

Rails 7.1 Adds Support for MessagePack as Message Serializer
source link: https://blog.saeloun.com/2023/11/15/rails-7-1-message-pack-as-message-serializer/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rails 7.1 Adds Support for MessagePack as Message Serializer
Nov 15, 2023Gowsik Vivekanandan
I’m a Ruby on Rails and Javascript enthusiast with over three years of experience in creating SaaS applications that make remote work easier. I work well in teams and am always up for new challenges. Born and raised in Namakkal, I enjoy playing video games, binge-watching tv shows, and stargazing to kill time.
In Rails 7.1, support for MessagePack has been added, and it can be used with MessageEncryptor
and MessageVerifier
. config.active_support.message_serializer
will also accept :message_pack
and :message_pack_allow_marshal
as serializers.
What is MessagePack?
MessagePack is an efficient binary serialization format that enables the exchange of data among multiple languages, similar to JSON.
It is faster and more compact compared to JSON, as it is optimized for binary data serialization. Specifically designed for efficiently representing complex data structures, it makes the payload smaller and faster to serialize and deserialize.
The following is a message serialized by MessagePack
:
require 'msgpack'
msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03"
MessagePack.unpack(msg) #=> [1,2,3]
Unlike BSON, which is a similar serialization format and less verbose, BSON is designed for faster in-memory manipulation, whereas MessagePack is designed for efficient network communication.
Before
Before Rails 7.1, Rails didn’t have native support for serializing using MessagePack. We had to install the msgpack gem and serialize the message.
# app/services/secure_message_service.rb
class SecureMessageService
def self.encrypt(message)
message_encryptor.encrypt_and_sign(message)
end
def self.decrypt(encrypted_message)
message_encryptor.decrypt_and_verify(encrypted_message)
end
private
def self.message_encryptor
secret_key = ActiveSupport::KeyGenerator.new(Rails.application.secret_key_base).generate_key('salt', 32)
ActiveSupport::MessageEncryptor.new(secret_key, serializer: MessagePack)
end
end
# rails c
> encrypted_message = SecureMessageService.encrypt([{:key=>"value"}, [1, 2, 3], {:a=>{:b=>1}}])
=> "AaeKemIH/k7moSB8kgGvsNPJNr7MY1vUlMlwGBk=--AMViwItIvTcwz9Xp--NTMQJJrKKgdA1y1qt/KuWA=="
> SecureMessageService.decrypt(encrypted_message)
=> [{:key=>"value"}, [1, 2, 3], {:a=>{:b=>1}}]
To use MessagePack with ActiveSupport::MessageEncryptor
, we need to pass serializer argument with the value MessagePack
. This ensures that the messages are serialized and deserialized appropriately during both encryption and decryption processes.
After:
MessagePack is supported starting from Rails 7.1, with msgpack integrated into it. Rails has also introduced a new class called ActiveSupport::MessagePack
, designed to serialize most basic Ruby data types.
To configure MessagePack as the default serializer in the Rails application, include the following in config/application.rb
:
# config/application.rb
config.active_support.message_serializer = :message_pack
In Rails 7.1, the default serializer is json_allow_marshal
. However, it can fall back to deserializing with Marshal
so that legacy messages can still be read. We can set MessagePack as the default serializer in the app by using the config.active_support.message_serializer
configuration method and setting the value as message_pack
.
# app/services/secure_message_service.rb
class SecureMessageService
def self.encrypt(message)
message_encryptor.encrypt_and_sign(message)
end
def self.decrypt(encrypted_message)
message_encryptor.decrypt_and_verify(encrypted_message)
end
private
def self.message_encryptor
secret_key = ActiveSupport::KeyGenerator.new(Rails.application.secret_key_base).generate_key('salt', 32)
ActiveSupport::MessageEncryptor.new(secret_key)
end
end
Since MessagePack is configured as the message serializer using the config.active_support.message_serializer
method, we do not need to pass the serializer argument to ActiveSupport::MessageEncryptor
as we did in the previous Rails version.
# rails c
> encrypted_message = SecureMessageService.encrypt([{:key=>"value"}, [1, 2, 3], {:a=>{:b=>1}}])
=> "AaeKemIH/k7moSB8kgGvsNPJNr7MY1vUlMlwGBk=--AMViwItIvTcwz9Xp--NTMQJJrKKgdA1y1qt/KuWA=="
> SecureMessageService.decrypt(encrypted_message)
=> [{:key=>"value"}, [1, 2, 3], {:a=>{:b=>1}}]
> ActiveSupport::Messages::Codec.default_serializer
=> :message_pack
MessageEncryptor
will serialize the message using MessagePack
during both encryption and decryption processes. To check the default serializer, we can run ActiveSupport::Messages::Codec.default_serializer
, which returns message_pack
.
Along with message_serializer, MessagePack can also be used as cookies_serializer and cache_serializer.
Share this post!
Recommend
-
63
Netty中使用MessagePack时的TCP粘包问题与解决方案
-
65
MessagePack在Netty中的应用
-
106
README.md MessagePack A MessagePack encoder and decoder for Codable types. This functionality is discussed in Chapter 7 of
-
14
Ultra tiny object serializer Disclaimer: This library is part of a bigger project and it's goal is to be as small as...
-
42
MessagePack for C# (.NET, .NET Core, Unity, Xamarin) The extremely fast MessagePack serializer for C#. It is 10x faster than MsgPack-Cli and outpe...
-
10
MessagePack.FSharpExtensions MessagePack.FSharpExtensions is a MessagePack-CSharp extension library for F#. Usage open System open System.Buffers open...
-
7
Rails 7 adds range serializer for ActiveJob May 25, 2021 , by Alkesh Ghorpade 1 minute read When using Rails, we often need...
-
3
MessagePack.FSharpExtensions MessagePack.FSharpExtensions is a MessagePack-CSharp extension library for F#. Usage open System open System.Buffers open...
-
11
Optimizing network footprint using MessagePackMeasure space and time improvement when using MessagePack instead of JSON to send image over websocket. Jun 23, 2022 •
-
6
这篇文章给了简单的讲解和利用方式: https://blog.netwrix.com/2023/04/10/generating-deserialization-payloads-for-mess...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK