Using message pack with ASP.NET Core SignalR
September 04, 2018 by Anuraj
ASPNET Core SignalR MessagePack
This post is about using MessagePack protocol in SignalR. MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it’s faster and smaller. Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.
To use MessagePack, you need to install a nuget package - Microsoft.AspNetCore.SignalR.Protocols.MessagePack
. You can install the package using dotnet add package Microsoft.AspNetCore.SignalR.Protocols.MessagePack --version 1.1.0-preview1-35029
command.
Once you install, you can modify the startup code like this.
In this post I am using simple chat implementation of SignalR. Next you need to install the client packages. You need to MessagePack protocol implementation of javascript as well. Here is the project.json file, you can either install the packages individually or you run npm install
command to install the packages.
And you can modify the _layout.cshtml
file like this. Please note the order of script files is important. If signalr-protocol-msgpack.js is referenced before msgpack5.js, an error occurs when trying to connect with MessagePack. signalr.js is also required before signalr-protocol-msgpack.js.
To copy the files from node_modules
folder, I am using the MSBuild steps, like this.
You need to modify the script like this, to connect to MessagePack enabled SignalR hub.
As of now there are no configuration options for the MessagePack protocol on the JavaScript client.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub