Server broadcasts not working

Issue #12 resolved
Daniel Frömmel created an issue

Hi there,

i want to broadcast a message via SignalR to the connected clients but the javascript method is never called (i tried the following tutorial: http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr)

Here are the steps to reproduce the issue:

1) Get your plugin up and running in Orchard

2) Setup all the common stuff that is needed in the MVC view to get SignalR up and running (include jquery, signar, ...)

3) Create a client side method for SignalR in the MVC view

$(document).ready(
  function() 
  {
    var lNotificationHub = $.connection.notificationHub;

    lNotificationHub.client.receive = function (a) { alert(a); };
  }
);

4) Create a hub class in another module like

public class NotificationHub : Microsoft.AspNet.SignalR.Hub
{
  public void Send(string a)
  {
    Clients.All.receive(a);
  }
}

5) Add the following code to an action method

Microsoft.AspNet.SignalR.IHubContext context =
  Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<NotificationHub>());

context.Clients.All.receive("a");

6) Invoke the action method

When the action method is invoked the context is returned correctly but the "receive" call never gets through to the clients. I tried the same code from a default web application created by Visual Studio and it works.

I searched through your module and you are doing something with the dependency resolver and most likely thats the cause as a same issue is described here: http://stackoverflow.com/q/20561196/3936440

Is there a way to make server broadcasts working with your module?

Thx & So lonG

Comments (4)

  1. Piotr Szmyd

    This is not actually a bug but something I haven't apparently described well. You shouldn't use the Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager (and other global statics) - this won't work. Instead, inject IConnectionManager and use this instance instead.

    The main reason for this is multitenancy, which is Orchard core concept. Each tenant (shell) needs to have its own instance of all SignalR components (like eg. ConnectionManager) available, hence global singletons don't make much sense. You can't tell in context of which tenant they are executing. Instead - use dependency injection and fetch whatever component you need this way.

    Actually, I should've made this more explicit and throw an exception with some meaningful information when those globals are used (instead of failing silently).

  2. Piotr Szmyd

    Use dependency injection and inject IConnectionManager instead of using global singleton Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.

  3. Daniel Frömmel reporter

    Thank you for your detailed response. I didn't see that the same question was already posted here. It's working now.

  4. Log in to comment