Skip to content

SignalR JS Client Hubs

davidfowl edited this page Nov 5, 2011 · 26 revisions

SignalR Javascript Client (Hubs)

Setup

Include the following scripts on your page:

<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>
<script src="signalr/hubs" type="text/javascript"></script>

NOTE: In an MVC application use Url.Content to reference scripts.

Automatic Proxy Generation

If you navigate to signalr/hubs in your browser, you'll see a script that is dynamically generated based on the hubs declared on the server. Each hub on the server will become a property on the client side $.connection.

How it works

SignalR registers some code to run at application start time that finds all hubs in your application. On the first request to signalr/hubs, the proxy is generated and cached for the lifetime of the application.

Programming Model

$.connection.hubName

Access a client side hub from the generated proxy.

  • Returns a Hub.
  • NOTE: The name of the hub is camel cased (i.e. if the server Hub is MyHub the property on connection will be myHub)

Example

var myHub = $.connecton.myHub;

hub.eventName = callback

Subscribes to an event that the server can call.

  • NOTE: The eventName cannot be a method that exists on the server hub (i.e. if the server has a method called Send, you cannot define a client event called send).

Example

myHub.addMessage = function(value) {
    console.log('Called addMessage(' + value + ')');
};

Clone this wiki locally