sports
sports

Reputation: 8147

SignalR: connect to one hub from javascript, not to all of them

The following connects the js client to all the existent hubs:

$.connection.hub.start({ transport: 'longPolling' }).done(function () {});

Is there some way to connect to some particular hub instead?

If not, what is the point of "OnConnected()" and "OnDisconnected()" being in EVERY hub? if all of them are going to be called anyway.

I'm asking this because there is a particular html (something like a news feed) in which I want to handle notifications from one hub (when news are posted, this view should handle and show them appearing). I'm going to use groups but seems more natural to separate this in hubs.

Upvotes: 1

Views: 274

Answers (3)

N. Taylor Mullen
N. Taylor Mullen

Reputation: 18301

You only subscribe to hubs that you have client side events associated with. So lets say you had two hubs:

  1. hubA
  2. hubB

In your client if you have:

var hubA = $.connection.hubA;

hubA.client.foo = function() {};

$.connection.hub.start();

You will only be subscribed to events on hubA (not hubB).

Upvotes: 2

Colin Swelin
Colin Swelin

Reputation: 841

var connection = $.hubConnection(server);
var someHub= connection.createHubProxy("someHub");
connection.start({transport: 'longPolling'}).done(function () {})

Upvotes: 0

Julien
Julien

Reputation: 282

You can choose to use or not use the connection events in any hubs you create. You certainly can separate your code into logical sections using hubs, no harm in that.

Upvotes: 0

Related Questions