Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Hubs in SignalR (Design)

Tags:

signalr

Using SignalR, I am wondering what the best way to set up my Hubs is under the following scenario: Say I have a web casino app (just for fun) and it has three games, Poker, Blackjack, and Slots. Poker and Blackjack are both multi-player so they have a chat feature and Slots does not. Okay, to support this I was thinking of setting up my Hubs in the following way.

BaseHub (Handles connection stuff that is common to Poker, Blackjack, and Slots)

PokerHub : BaseHub (Handles Poker game play)

BlackjackHub : BaseHub (Handles Blackjack game play)

SlotsHub : BaseHub (Handles Slots game play)

ChatHub (Handles chat features)

I was the thinking of having the Poker page of this web app connect to the PokerHub as well as the ChatHub and the Blackjack page would do something similar. The Slots page would obviously only connect to the SlotsHub.

Now, The things I am unsure about are: Should the Poker/Blackjack pages connect to both the PokerHub/BlackjackHub and the ChatHub or is there some way I could have them only connect to the PokerHub/BlackjackHub and delegate the chat features to the Chat hub? In that case I might create like an interface IHasChat or something like that. In either case should the ChatHub also extend the BaseHub? Currently the BaseHub only implements IConnected, IDisconnect and also handles basic Group functions (JoinGroup, LeaveGroup). Also, should the BaseHub be a shared instance (singleton)?

Lastly, if you think I am just going about it totally wrong please let me know. This is my first SignalR project and I know I am not an expert on it. Also, I know that there are actually several questions here. If you can answer any or all of them, either way I really appreciate it.

Thank You, Tom

like image 264
tleef Avatar asked May 29 '12 05:05

tleef


People also ask

What are hubs in SignalR?

Hubs are SignalR's high level API that allow both realtime client-to-server and server-to-client RPC over HTTP. The hub supports 1-to-many RPC e.g.: all clients, groups of clients, just the caller etc.

How do I add a class to hub SignalR?

Or you can also add SignalR to a project by opening the "Tools" | "Library Package Manager" | "Package Manager Console" and running the command: "install-package Microsoft. AspNet. SignalR". Now again add the SignalR class.

Is SignalR asynchronous?

SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.


2 Answers

You can have as many hubs as you like as there is only ever one connection to the SignalR server. Hubs are an RPC implementation and all share the same connection. The Wiki page on hubs for the js client shows a connection like so:

$.connection.hub.start()

Where hub is a namespace inside the js client.

like image 191
el_tone Avatar answered Sep 20 '22 13:09

el_tone


Practically it never requires to have multiple hub classes on server. Its like you need multiple internet connections. One for surfing Games, Another for Social Media and third one for something different.

Create a single Hub Class on server side.

What you should do is have different Clients using JS.

For example in my website I had two things. Live chat and Offline notifications. For chat.aspx i had a variable called "chat" acting as Hub. For all other pages i had a variable called NotificationHub.

You can do something like below

var pocker = $.connection.hub;
var blackJack = $.connection.hub;
var other = $.connection.hub;

This way call the respective methods Also if you want to identify who called your server method, you can attach query string parameter.

pocker.connection.qs = "type=pocker";
like image 43
Ronak Patel Avatar answered Sep 19 '22 13:09

Ronak Patel