Introducing MessagePack Protocol
Learn to enable MessagePack on the server.
Overview
The MessagePack protocol is not exclusive to SignalR. It’s similar to JSON in its structure but binary rather than textual. So, we'll have the same types of fields and the same object structure, but we won’t be able to read the message while it’s being transferred easily. However, at the same time, the fact that the message is binary makes it much smaller. Therefore, it will get transferred faster.
Enabling MessagePack on the server
MessagePack has not been included in the default SignalR packages. To enable it on the server, we will need to install a NuGet
package, which can be achieved by running the following command inside the SignalRServer
project folder:
dotnet add package Microsoft.AspNetCore.SignalR.Protocols.MessagePack
Then we will open the Program.cs
file inside the project and will add the following namespace reference:
using MessagePack;
After this, we will locate the AddSignalR
method call. We can then either replace AddJsonProtocol
or add the following call to it:
.AddMessagePackProtocol(options =>{options.SerializerOptions = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData).WithCompression(MessagePackCompression.Lz4Block).WithAllowAssemblyVersionMismatch(true).WithOldSpec().WithOmitAssemblyVersion(true);});
So, this is how we enable MessagePack
on the server. We don’t have to change any settings, as the default settings would be sufficient for most scenarios. But this code demonstrates what settings are available if we need to fine-tune it. Let’s go through them all.
SerializerOptions
: This setting allows us to apply custom serialization logic to our MessagePack messages. It’s represented by the ...