Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to communicate Server to Client with SignalR in Nancy with ASP.NET Hosting?
    primarykey
    data
    text
    <p>Most of the examples I've found for <a href="http://signalr.net/" rel="nofollow noreferrer">SignalR</a> are assuming ASP.NET (MVC or not). I'm using <a href="http://www.nancyfx.org" rel="nofollow noreferrer">NancyFX</a>. I'm having just one problem, so I'm hoping there's something I'm overlooking or some thing I need to do in Nancy to compensate for not being ASP.NET.</p> <p>My one goal is to be able to notify the client browsers when a server event happens. I don't plan on replacing my Nancy routes with hub methods. But I would like the ability to call into the browser from my routes (actions). </p> <p>I have very simple Hub that I created following the example in the <a href="https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs" rel="nofollow noreferrer">SignalR Wiki</a>. I'm not even sure I need it, since I don't plan on calling client to server.</p> <pre><code>public interface IUserNotifier { void Start(); void Notify(object @event); } </code></pre> <p>I used an interface in hopes that I would be able to inject the same hub later on to use in my nancy routes... I'm not sure that is in the cards.</p> <pre><code>[HubName("userNotifier")] public class UserNotifier : Hub, IUserNotifier { public void Start() { Notify(new {Status = "Started"}); } public void Notify(object @event) { Clients.notification(@event); } } </code></pre> <p>When I have the following code in my html file, I can see that it executes the <code>Start()</code> method, and then the <code>Notify()</code> method, delivering content to my client.</p> <pre><code>var communicator = $.connection.userNotifier; $.extend(communicator, { Notification: function(event) { alert("notification received from server!"); console.log(event); } }); $.connection.hub.start() .done(function() { communicator.start(); }); </code></pre> <p>Like I said, "starting" the hub works and sends a notification to the client. Very cool. But, then, my primary goal hasn't been accomplished yet. I need to initiate these notifications from other places in my code where they might not be directly associated with a "request".</p> <p>I tried injecting my IUserNotifier in my nancy modules for use in the routes, but when the <code>Notify()</code> is fired, I get:</p> <p><img src="https://i.stack.imgur.com/YSB1k.png" alt="Cannot perform runtime binding on a null reference"></p> <p>That's because the <code>Clients</code> property on the <code>Hub</code> base class is null (hasn't been initialized). So, I switched gears. I tried to follow multiple examples, including the example from the <a href="https://github.com/SignalR/SignalR/wiki/Hubs" rel="nofollow noreferrer">wiki page about hubs</a> in the section called "Broadcasting over a Hub from outside of a Hub":</p> <pre><code>public class NotifierModule : NancyModule { public NotifierModule(){ Get["/notify/{message}"] = p =&gt; { var context = GlobalHost.ConnectionManager.GetHubContext&lt;UserNotifier&gt;(); context.Clients.notification(new { Message = p.message }); }; } } </code></pre> <p>My Nancy route executes without throwing errors. Except my browser never receives the message. If I set a breakpoint in the route, I can see that <code>Clients</code> is initialized. Maybe the collection of clients is initialized but empty. Who knows? Maybe you do. :)</p> <p>Again, my main goal is to be able to send events/notifications to the browser from anywhere in my code, any time. Is that too much to ask? What should I be doing here? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload