The previous recipe taught us how to call a server-side method exposed by a Hub and how to receive its return value, if any. As we already explained in Chapter 2, Using Hubs, SignalR really shines when we have to push information to several connected clients at the same time, and we illustrated the various APIs available to do this. In this recipe, we'll concentrate again on those strategies, and in particular on notifying the caller, but our focus will be on explaining the client-side mechanics involved when receiving server-to-client calls.
We need to prepare our Hub, and to do this we need to perform the following steps:
EchoHub
.app.MapSignalR();
inside its Configuration()
method.For more details on these steps, you can check the previous chapters.
Then, let's start adding the code for this specific sample by performing the next set of steps:
EchoHub
and make it look like the following code: using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
[HubName("echo")]
public class EchoHub : Hub
{
public void Say(string message)
{
Clients.Caller.greetings(message);
}
}
The content is trivial and similar to many samples that we saw in Chapter 2, Using Hubs. Our Say()
method is just pushing the supplied message
parameter back to the caller. We could have pushed it to any of the several connection sets available, which we saw earlier. If you want to change this particular aspect of this recipe, please go back to Chapter 2, Using Hubs, for some inspiration.
The highlighted portion of the preceding code is the invocation of the greetings()
method. We already saw in Chapter 2, Using Hubs, how such a method call is dynamically resolved.
index.html
.<script src="Scripts/jquery-2.1.0.js"></script> <script src="Scripts/jquery.signalR-2.0.2.js"></script> <script src="/signalr/hubs"></script>
<script> ... </script>
$(function() { ... });
hub
object and the echo
Hub proxy, whose roles have been already explained in the previous recipe. This is shown in the following code:var hub = $.connection.hub; var echo = $.connection.echo;
client
member of the echo
object to dynamically add a greetings()
function expecting one parameter, as shown in the following code:echo.client.greetings = function(message) { $('#message').html(message); };
This signature corresponds to that of the method invoked from inside the Hub method on the Clients.Caller
member, as illustrated in step 1. SignalR will wire this definition when the connection starts, and it will be able to match it to the corresponding server-side call so that the invocation from inside the Hub method triggers a call to the client. The received argument will have the same value as the value that is used during the server-side invocation.
All these callback definitions must be added to the client
member. It's interesting to note that while these definitions go all together inside this specific member of the proxy, the corresponding server-side invocations can be triggered from any of the several Clients
members, such as Caller
or Others
; each one of them corresponds to a different set of connected clients as explained in Chapter 2, Using Hubs.
The actual body of the client-side function is not very important. It just uses jQuery to find a specific DOM element that is filled with the value carried by message
.
var started = hub.start();
The start()
call returns a JavaScript promise
object that we'll use to set up a function to be called back when the connection is ready. It's this call that wires our client-side greetings()
function so that the server-side call matches it.
started.done(function() { echo.server.say("hello!"); });
The done()
method and the server
member have already been explained in the previous recipe. What is going on here is just a simple call to the server-side Say()
method and performed inside the done()
callback. We ensure that it's triggered when the connection startup has been completed successfully. The Say()
method will be called on the server, and from there the greetings()
invocation will go back to the calling client.
Again, we wrote some pretty verbose code to illustrate these steps. The exercise of making it simpler and more synthetic is left to you!
If we now visit the index.html
page, we will see a hello! message printed on the page as a result of a process where the following actions were performed:
done()
callback).Clients.Caller
member).message
value inside the greetings()
method that we added to the echo.client
member and print it.