Receiving SignalR messages from PowerShell

A couple of days ago I wrote about how to use RabbitMQ with AMQP from PowerShell (via a small assembly that utilized the original C# .NET Client). After I implemented it in the use case for our customer I somehow felt it to be “too heavy” for what I was really using it (compared to all the features AMQP has to offer to you). And as we were already using SignalR in some other client projects I wondered if I could use this as a replacement for AMQP. Before you scream out and complain that SignalR was never meant to be a replacement or even comparable to a much superiour protocol like AMQP – relax. I know. Still – I wanted to have a quick look to see if and how it might fit. So after a quick look the most prominent drawback I had to complain about was that you cannot just “dequeue” an element from a queue with multiple workers waiting for the same queue. SignalR would just send it happily to all of them. Luckily this was not an issue in my scenario as I had a central dispatcher that would wait for messages. On the positive side I noticed it would be perfectly and easily usable via multiple web servers and work across all in-between firewalls as the connection to my web servers were already allowed. And there was a .NET/C# client for it as well available on NuGet Microsoft ASP.NET SignalR .NET Client 2.1.1.

Install-Package Microsoft.AspNet.SignalR.Client

Furthermore I could run it without installing an additional server component, but I could have it side-by-side with my existing SignalR functionality the browser was already using. And really with a few lines of code mostly for convenience, it was readily usable from PowerShell:

In case your browser does not correctly render the Gist code you can view it directly at: https://gist.github.com/dfch/cc5a4b0145509c838477

I made most of the properties public so you can still use the underlying .NET objects. And I did not implement “groups” nor “sending” to the server, but just enough to “receive” messages. (For “sending” messages you can invoke the methods directly on the hub as shown in my next post.) Invocation from PowerShell was then pretty simple:

$eventName = "receiveMessage";
Add-Type -Path ".\SignalRClient.dll"
$s = New-Object SignalRClient.Connection("http://localhost/", "commandhub");
$s.Start($eventName);
while($true) {
	$s.TryDequeue($eventName)
	Start-Sleep -seconds 1
}
string TryDequeue(string eventName)
string Dequeue(string eventName)
string Dequeue(string eventName, int dwMillisecondsTotalWaitTime)
string Dequeue(string eventName, int dwMillisecondsTotalWaitTime, int dwMilliSecondsWaitIntervall)
System.Collections.Generic.List[string] DequeueAll(string eventName)

Note1: For ease of use there are several dequeueing methods defined. The “dwMilliSecondsWaitIntervall” is only used internally to “poll” the memory queue where all the messages are stored (so there is not server polling). Note2: In this example the current username is used to connect to the SignalR hub. Therefore no credentials are supplied. Using this approach instead of a full blown RabbitMQ installation really reduced the memory footprint of the application and eliminated an HA pair of servers including a separate database and an authentication system that was not Windows based.

Trackbacks

  1. […] is a quick addition to my previous blog post on how to receive SignalR messages via PowerShell. Once you have established a connection in PowerShell to your SignalR hub you can easily them a […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: