Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR Hubs in a class library? Is this a good idea or a bad one?

I'm fairly new to SignalR. I started writing a hub and putting that logic in a class library.

Here's what I'm trying to accomplish:

I have a "preprocessor" service that goes off and pre-fetches a number of objects/documents/etc from external sources and puts them into the cache. I am using the hub to communicate to clients when all items have been pre-fetched.

We also have several client consumers. Among them include:

  • a typical MVC app/project
  • a console app we use for quick testing
  • a webapi and a WCF/SOAP project for exposing our services to external parties
  • a unit test project

The problem

By putting the hub in the class library, I've made all the downstream consumers (including other class libraries) have a dependency on OWIN... and I now need my startup class (and or app config settings to tell Owin to not use it).

public class Startup
{
   public void Configuration(IAppBuilder app)
   {
      app.MapSignalR();
   }
}

or

<add key="owin:AutomaticAppStartup " value="false" />

The Architecture?

This 'smells' to me. Maybe SignalR hubs are not meant to be in a class library? If that is the case, what would be a better design? e.g. have the hub in its own WebApi Service? And proxies to this service? That way, my preprocessor logic (contained in a class library) can call the Hub?

Suggestions would be greatly appreciated.

like image 715
Raymond Avatar asked Jun 02 '14 17:06

Raymond


People also ask

When should I use SignalR?

SignalR can be used to add any sort of "real-time" web functionality to your ASP.NET application. While chat is often used as an example, you can do a whole lot more. Any time a user refreshes a web page to see new data, or the page implements long polling to retrieve new data, it is a candidate for using SignalR.

What are SignalR hubs?

What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.

Why do we need SignalR?

SignalR is a free and open-source software library for Microsoft ASP.NET that allows server code to send asynchronous notifications to client-side web applications. The library includes server-side and client-side JavaScript components.

Is SignalR secure?

SignalR uses encryption and a digital signature to protect the connection token. For each request, the server validates the contents of the token to ensure that the request is coming from the specified user.


1 Answers

You can have your SignalR stuff in a class library for sure, but in your case that should probably be a different one from the one you have where the preprocessing happens. The SignalR one would be pure infrastructure, and no business logic would happen, just messaging. You can define some interface in the middle and plug the infrastructure into your preprocessing one. This way the dependency on SignalR is behind an interface, your code is clean, and the dependency on Owin is isolated into the infrastructure.

like image 91
Wasp Avatar answered Sep 22 '22 06:09

Wasp