-
Notifications
You must be signed in to change notification settings - Fork 2.2k
SignalR JS Client Hubs
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.
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, e.g. $.connection.myHub.
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.
The connection for all hubs (url points to /signalr).
- Returns a connection
The client id for the hub connection.
Set to true to enable logging. Default is false
Starts the connection for all hubs.
- See other overloads to start()
- Returns jQuery deferred
Example
$.connection.hub.start()
.done(function(){ alert("Now connected!"); })
.fail(function(){ alert("Could not Connect!"); });Access a client side hub from the generated proxy.
- Returns a Hub.
- hubname - Name of the hub on the server.
- 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 = $.connection.myHub;The javascript client can declare methods that the server can invoke. To expose these methods, declare a function on the client side hub.
- NOTE: The function on the client side hub 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).
Declares a function the server can invoke.
-
method - name of the client side method.
-
callback - A function to execute when the server invokes the method.
-
NOTE: In the callback, this points to the hub instance.
-
NOTE: if you misspell you will NOT get any warning or notifications even when logging is enabled.
-
NOTE: Unlike the name change in myHub, the function names here at the same as you call in Server code.
Example
myHub.client.addMessage = function(value) {
console.log('Server called addMessage(' + value + ')');
};To set state on the hub. Just assign values to properties on the hub object:
myHub.state.name = "David";Whenever a call to the hub the state will be sent to the server.
Similarly, you can access state set on the server using myHub.property:
var age = myHub.state.age;The proxy will generate methods on each hub for the associated server methods. You can invoke these methods to call a server side hub method.
- Returns jQuery deferred
- NOTE: Method names will be camel cased similarly to hub names
Example
myHub.server.someMethod()
.done(function(result) {
})
.fail(function(error) {
});$.connection.hub.logging = true;
var myHub = $.connection.myHub;
myHub.state.someState = "SomeValue";
function connectionReady() {
alert("Done calling first hub serverside-function");
};
myHub.client.SomeClientFunction = function() {
alert("serverside called 'Clients.SomeClientFunction()'");
};
$.connection.hub.error(function() {
alert("An error occured");
});
$.connection.hub.start()
.done(function() {
myHub.server.SomeFunction(SomeParam) //e.g. a login or init
.done(connectionReady);
})
.fail(function() {
alert("Could not Connect!");
});You can talk to SignalR servers either using websockets, cors enabled longpolling (not supported by all browsers) or jsonp longpolling.
Cross domain urls are auto detected. We'll use xhr by default if the client (your browser) supports it. Otherwise it'll fall back to jsonp longpolling.
$.connection.hub.url = 'http://localhost:8081/signalr'
$.connection.hub.start();To use jsonp longpolling, you can specify that option explicitly:
$.connection.hub.url = 'http://localhost:8081/signalr'
$.connection.hub.start({jsonp: true});