Install-Package Microsoft.AspNet.SignalR -Version 1.1.3.
You can also use newer versions of SignalR.
In the webapi controller that handles the creation of Geotopics, I also update all the clients with this newly created Geotopic.
The SignalR hub is lazy loaded by the Geotopic webapi controller.
protected readonly Lazy
new Lazy
The webapi method Post is slightly modified and calls the posted method on the clients.
[System.Web.Http.Authorize]
public void Post(Geotopic value)
{
//user must be logged in.
var userNode = client.QueryIndex
//now create a topic
Geotopic newTopic = new Geotopic(value.latitude, value.longitude, value.message);
var secondtopic = client.Create(newTopic,
new IRelationshipAllowingParticipantNode
new[]
{
new IndexEntry("Geotopic")
{
{ "Id", newTopic.id.ToString() }
}
});
NodeReference
client.CreateRelationship(secondtopic, new PostedBy(reference));
//now SignalR it to all my followers....
AdminHub.Value.Clients.All.posted(value);
}
A piece of Javascript makes all of the above happen. It connects to the geotopichub and responds to the "posted" call from the webapi controller.
To show a nice popup on the Geotopiascreen I use Toastr. Get this by: install-package Toastr.
When I post a topic, the Toastr package displays a nice toast:
The SignalR addition enables webclients to be updated on the fly. For future release of mobile clients of Geotopia, I also use the Notification Hub of Windows Azure to enable notifications on mobile devices. To enable this, I create a service bus namespace and a notification hub.
Now I have a notificationhub available. A mechanism to easily update my mobile clients. On the backend side (my webrole), install the service bus package.
Install-Package WindowsAzure.ServiceBus
add use it in your designated class. In my case, I use it in my TopicsController.cs webapi module.
using Microsoft.ServiceBus.Notifications;
I add the following lines to the Post method of my TopicsController:
NotificationHubClient myNotificationHub = NotificationHubClient.
CreateClientFromConnectionString("Endpoint=sb://
"YOUR_HUB");
string toast = "" +
"
"
"
"
" + "
";
Task
In the result we can see the outcome of the notification call.
For now, everybody receives notifications of topics being posted. The next blog post will demonstrate how to make sure only my followers get my topics by using SignalR groups and adding tags for the Notification Hub.
No comments:
Post a Comment