I'd like to know if I can add SignalR messages directly to the SignalR SQL Backplane (from SQL) so I don't have to use a SignalR client to do so.
My situation is that I have an activated stored procedure for a SQL Service Broker queue, and when it fires, I'd like to post a message to SignalR clients. Currently, I have to receive the message from SQL Service Broker in a seperate process and then immediately re-send the message with a SignalR hub.
I'd like my activated stored procedure to basically move the message directly onto the SignalR SQL Backplane.
Yes and no. I set up a small experiment on my localhost to determine if possible - and it is, if formatted properly.
Now, a word on the
[SignalR]schema. It generates three tables:Right, so, I duplicated the last column except I incremented the
PayloadId(for the new record and in[Messages_0_Id]and put inGETDATE()as the value forInsertedOn. Immediately after adding the record, a new message came into the connected client. Note thatPayloadIdis not an identity column, so you must manually increment it, and you must copy that incremented value into the only record in[Messages_0_Id], otherwise your signalr clients will be unable to connect due to Signalr SQL errors.Now, the trick is populating the [Payload] column properly. A quick look at the table shows that it's probably binary serialized. I'm no expert at SQL, but I'm pretty sure doing a binary serialization is up there in difficulty. If I'm right, this is the source code for the binary serialization, located inside
Microsoft.AspNet.SignalR.Messaging.ScaleoutMessage:With
WriteTo:So, re-implementing that in a stored procedure with pure SQL will be near impossible. If you need to do it on the SQL Server, I suggest using SQL CLR functions. One thing to mention, though - It's easy enough to use a class library, but if you want to reduce hassle over the long term, I'd suggest creating a SQL Server project in Visual Studio. This will allow you to automagically deploy CLR functions with much more ease than manually re-copying the latest class library to the SQL Server. This page talks more about how to do that.