Most of the examples I found for SignalR accept ASP.NET (MVC or not). I am using NancyFX . I have only one problem, so I hope I miss something or something I need to do in Nancy to make up for the lack of ASP.NET.
My only goal is to be able to notify client browsers when a server event occurs. I do not plan to replace my routes with Nancy hubs. But I would like you to be able to call the browser from my routes (actions).
I have a very simple Hub that I created after the example in the SignalR Wiki . I'm not even sure that I need this, since I do not plan to call the client on the server.
public interface IUserNotifier { void Start(); void Notify(object @event); }
I used the interface in the hope that later I could introduce the same hub for use in my nancy routes ... I'm not sure if this is in the cards.
[HubName("userNotifier")] public class UserNotifier : Hub, IUserNotifier { public void Start() { Notify(new {Status = "Started"}); } public void Notify(object @event) { Clients.notification(@event); } }
When I have the following code in my html file, I see that it executes the Start() method and then the Notify() method, delivering the contents to my client.
var communicator = $.connection.userNotifier; $.extend(communicator, { Notification: function(event) { alert("notification received from server!"); console.log(event); } }); $.connection.hub.start() .done(function() { communicator.start(); });
As I said, the βlaunchβ of the hub works and sends a notification to the client. Very cool. But then my main goal is not yet complete. I need to initiate these notifications from other places in my code, where they may not be directly related to the βrequestβ.
I tried to insert my IUserNotifier into my nancy modules for use in routes, but when Notify() fired, I get:

This is because the Clients property in the base Hub class is null (not initialized). So, I switched gears. I tried to run several examples, including an example from the hub wiki page in the section "Broadcasting through the hub from the hub" ":
public class NotifierModule : NancyModule { public NotifierModule(){ Get["/notify/{message}"] = p => { var context = GlobalHost.ConnectionManager.GetHubContext<UserNotifier>(); context.Clients.notification(new { Message = p.message }); }; } }
My Nancy route runs without errors. In addition, my browser never receives a message. If I set a breakpoint on the route, I will see that Clients initializing. The customer collection may be initialized, but empty. Who knows? Maybe so.:)
Again, my main goal is to be able to send events / notifications to the browser from anywhere in my code at any time. Am I asking so much? What am I supposed to do here?