4

I am building an instant chat module for the orchard project I am working on. I would like to use SignalR to power the chat, however I am running into difficulties with this:

<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>

I receive 404 error with everything I try. I have RAMFAR in webconfig, I have tried rewritting the url on IIS. I am hoping it is something horribly simple that my addled brain cant quite see. I don't even know for sure that SignalR works with Orchard, I suspected there would be some difficulties though

tereško
  • 58,060
  • 25
  • 98
  • 150
Hazza
  • 6,441
  • 3
  • 24
  • 37

2 Answers2

6

It's much more tricky than @mdm described... SignalR uses a custom HttpModule that handles hubs. Unfortunately, due to dynamic loading of module assemblies there is no way to wire that HttpModule from a module without altering Orchard core.

Fortunately, I managed to sort things out and made a live demo with Orchard and SignalR playing nice together! Making it work was quite tricky and involved dropping the HttpModule in favor of a custom route implementation. I'll push the module to Orchard Gallery after doing some necessary cleanup and post an update here.

UPDATE: Module is nearly finished. There was a lot of changes in SignalR library since I started so needed to incorporate those. Also, there is one slight change to Orchard core (session handling) that I need to push before actually publishing the module.

UPDATE 2: SignalR uses Owin now so it's much easier to wire everything up. Orchard module is available here. You can also grab the latest code from BitBucket. Btw, I highly recommend using code from Orchard 1.x branch for SignalR development due to database transaction handling changes.

Piotr Szmyd
  • 13,371
  • 6
  • 44
  • 61
  • Pszmyd I love you. You are my hero. Really looking forward to this. :D – Hazza Mar 09 '12 at 01:23
  • Could you share this module with us ? I dont find it in the URL you gave above. – Anuj Pandey Apr 28 '12 at 08:50
  • @e10 Fixed the link. I'll publish the module when it's finished. Was waiting for SignalR 0.5 to go live before continuing work on the module - it needed some changes from 0.5. I guess I'll push the initial version somewhere around the end of next week. – Piotr Szmyd Apr 29 '12 at 16:14
  • Nice work. Is the source for this module available anywhere. ie. Open Source ? – Anuj Pandey Apr 30 '12 at 12:32
  • It will be, for sure. I'm changing a lot of things right now and don't want to publicize it until having something stable enough. Like I said - the end of this week is when the module will arrive. – Piotr Szmyd Apr 30 '12 at 12:46
  • @PiotrSzmyd Would you mind explaining what your module does exactly? I had a look at the source code but i'm pretty puzzled. – ViRuSTriNiTy Apr 28 '15 at 13:09
  • 1
    @ViRuSTriNiTy This module basically makes SignalR-based code work in Orchard without any custom configuration needed - does proper route registration with multitenancy support, add support for DI in connections/hubs etc. In other words - all the low level heavy-lifting. – Piotr Szmyd Apr 28 '15 at 15:17
  • @PiotrSzmyd Thanks, got your module up and running with Orchard 1.8.1. I had to put some major effort into it but it works, yay. Is it correct that the only thing i have to do now is to create a hub class derived from `Microsoft.AspNet.SignalR.Hub` and the rest is magically discovered / generated? SignalR tutorials include steps like creating a Startup class... – ViRuSTriNiTy Apr 29 '15 at 15:21
  • 1
    @ViRuSTriNiTy Exactly, it's the only thing you need to do. Hub/Connection classes are auto-discovered and registered accordingly. Module handles all the bootstrapping stuff needed for SignalR to run. – Piotr Szmyd Apr 29 '15 at 15:46
  • @PiotrSzmyd Kick-ass module i must say but still a few questions. a) Is there any way to map `signalr/hubs` to `~/Modules/MyModule/signalr/hubs` instead of `~/signalr/hubs`? b) Is there any way to broadcast messages from the server (action method) to clients (as `%HubContext%.Clients.All.%javascriptMethod%()` does not work)? – ViRuSTriNiTy May 04 '15 at 10:50
0

What is the URL that gets output on the page? What URL do you expect it to show?

Usually you would put the SignalR code in Scripts/signalr and reference scripts with the following at the top of your view:

@{
    Script.Include("signalr/script.js");
}

Or you could implement an IResourceManifestProvider in your module (Orchard.jQuery has a fantastic example of this, or see here), and then reference your scripts using Script.Require:

@{
    Script.Require("SignalR_Hubs");
}
Community
  • 1
  • 1
mdm
  • 12,480
  • 5
  • 34
  • 53
  • It is a dynamically created script you see, which I think is generated at application start time. The problem, from what I can make out, is that the process to generate this hub script is not being run by orchard at start up and so is never generated. Possibly am going to need to create a new module that is run pre application start to stop orchard dynamically loading it later which I assume is what is happening, or not loading it at all. I have tried adding signalr's dll to orchard.webs bin folder but it isn't playing ball. I will just keep digging, thanks for taking the time :) – Hazza Jan 31 '12 at 16:10
  • No worries :) I found this by coincidence: http://stackoverflow.com/questions/8053229/signalr-hub-not-loading-in-iis-7-but-working-correctly-in-visual-studio, it could be a web.config setting mismatch (maybe you are on IIS7 and looking at docs for IIS6 or vice-versa). As for whether SignalR would work with Orchard, I don't see why not - it is all ASP MVC and Orchard is quite happy for you to create controllers and use it as a framework for a larger app. – mdm Jan 31 '12 at 16:23
  • Yeah I have pretty much scoured the entire cyber world for information on this and indeed you would think that they would work perfectly together, but unless I have missed something obvious, they don't seem to. I plan to look at dependency injection tomorrow, autofac and IComponentRegistration, I can hardly wait... I must be doing something wrong here xD – Hazza Jan 31 '12 at 18:11
  • If it's a dynamically created script your best bet would be to create a Controller/Action in your module that would output the script, add a route to it and use `@Url.Action("Action", "Controller")` to get the script. – Piotr Szmyd Jan 31 '12 at 22:09
  • Going to leave it for now, I think orchard doesn't like the pre application start method that injects the http module used to set up the proxy hub script. Or something along those lines. I will come back to it when I feel like beating my head on the desk again. Or I realise that is was blindingly obvious and I was just being a douche. Hopefully the latter. Thanks for the input :) – Hazza Feb 01 '12 at 17:00
  • For what is it worth, Orchard and SignalR together are a bitch and currently it is not worth getting them to work together, they simply don't play nice. – Hazza Feb 25 '12 at 16:25