Reputation: 11478
So I've been diving into SignalR which is a really neat technology that has become apart of the ASP.NET family. I've followed a few tutorials, which explain how to build some neat real time applications such as a Stock Ticker, Chat, Dashboard, and Moving Objects.
These basic tutorials really show some of the power behind the framework. I was reading some documentation which showed:
So essentially you have two choices to wire up your Client and Server. But in my stumble through all of this documentation it states that:
SignalR can utilize Connections or Hubs; by drawing up with a Connection it will utilize Web Sockets. Which the coding into a raw socket comes with, but it is high-fidelity and low latency. But as I stated this can have drawbacks in either support for the Web Socket, coding directly to the socket.
Which makes valid sense, but then he describes how the Hub
works. Which is coded directly over a raw socket. This is where the interesting part is, the Hub
will apply the following transport methods:
Obviously the Hub
is pretty neat. As it identifies the best method of transport, then implements that method to accomplish the goal.
My question Why would you ever want to code directly to the socket, when the Hub
will automatically choose the best transport method anyways. Including the Web Socket?
The clarification would be nice. I don't quite understand why not use the Hub
? Why bother using Connection
?
Upvotes: 1
Views: 419
Reputation: 1007
Patrick Fletcher explains in Introduction to SignalR: Connections and Hubs why you might want to use Connections over Hubs. In all other cases, you should use Hubs as they give you a host of additional features. Here's what he wrote:
Connections vs. Hubs
The SignalR API contains two models for communicating between clients and servers: Connection and Hub.
A Connection represents a simple endpoint for sending single-recipient, grouped, or broadcast messages. The Connection API (represented in .NET code by the PersistentConnection class) gives the developer direct access to the low-level communication network that SignalR exposes. Using the Connections communication model will be familiar to developers who have used connection-based APIs such as Windows Communcation Foundation.
A Hub is a more high-level pipeline built upon the Connection API that allows your client and server to call methods on each other directly. SignalR handles the dispatching across machine boundaries as if by magic, allowing clients to call methods on the server as easily as local methods, and vice versa. If your application uses different types of messages, it is recommended that you use the Hub class, so that you won't have to create your own dispatching; using the Hub, you can call methods on the clients, rather than sending an explicit message that needs to be received, interpreted, and acted upon. Using the Hubs communication model will be familiar to developers who have used remote invocation APIs such as .NET Remoting.
Choosing a communication model
Most applications should use the Hubs API. The Connections API could be used in the following circumstances:
- The format of the message sent needs to be controlled.
- The developer prefers to work with a messaging and dispatching model rather than a remote invocation model.
- An existing application that uses a messaging model is being ported to use SignalR.
Upvotes: 3