introduction to signalr
Post on 08-Aug-2015
109 Views
Preview:
TRANSCRIPT
The entire contents of this document are subject to copyright with all rights reserved. All copyrightable text and graphics, the selection, arrangement and presentation of all information and the overall design of the document are the sole and exclusive property of Virtusa.
Copyright © 2012 Virtusa Corporation. All rights reserved
2000 West Park DriveWestborough MA 01581 USAPhone: 508 389 7300 Fax: 508 366 9901
Introduction to SignalR
SharePoint CEG | K-Talk
Shehan Peruma
August 23, 2013
2
Agenda
• Real-Time Web Technologies
• SignalR
• Getting Started
• Hubs API
• SignalR & SharePoint
• Summary
4
What?
• Stock Ticker
• Live Scores
• News Feeds
• Dashboards
• VoIP
• Chat
• Auctions
• Activity Streams
Improved User Experience & Server Performance
De f i n i t i on : an application that functions within a time frame that the user senses as immediate or current.
Real Time Web Technologies
5
How?
Multiple approaches exist to provide near real time functionality, but each has it’s own set of pros & cons.
• Polling
• Long Polling
• Forever Frame (IE only)
• Server Sent Events (SSE)
• Web Sockets
Real Time Web Technologies
Server
Client
Server
Client
6
Res
po
nse
4
Res
po
nse
3
Res
po
nse
2
Res
po
nse
1
Web Sockets – Quick Intro.
• Part of the Html 5 specification
• Full-duplex (unlike HTTP)
• Built on top of HTTP – can share HTTP ports (i.e. 80 & 443)
• URL: ws:// & wss:// similar to http:// & https://
• Client & Server support required
Real Time Web Technologies
Server
Client
Req
ues
t 1
Req
ues
t 2
Req
ues
t 3
Req
ues
t 4
Time
Fram
e 3
Fram
e 2
Fram
e 1
Han
dsh
ake
R
esp
on
se
Server
ClientH
and
shak
eR
eq
ues
t
Time
Fram
e 4
Po
llin
gW
eb
So
ckets
8
• Initially developed by members of the ASP.NET team
• Part of the ASP.NET Technologies
• Open source and hosted on GitHub
• Distributed via NuGet
• Requires .NET 4.0 and above
What is SignalR?
S i gna lR : An ASP.NET asynchronous, persistent signaling library that enables the real-time, multi-user web functionality for applications.
SignalR
14
Supported Clients & Hosts
Client
JavaScript
Silverlight 5
Windows Phone 8
.NETWinRT
iOS
Android
SignalR
Host
ASP.NET
Self-Hosting (OWIN)
17
Connections & HubsSignalR
Persisted Connections (low-level)
• Can communicate with 1 to N clients
• Is an IHttpHandler
• Requires a route to be defined
• Limited to sending messages
• You define the “protocol”
Hubs (high-level)
• Can communicate with 1 to N clients
• Abstraction over PersistentConnection
• Route automatically mapped (/signalr/hubs)
• Can send messages and call methods
• SignalR defines the protocol
18
Hubs
• Hub methods can be called by the clients (e.g. javascript)
• Client methods can be called by the Hub
• Can send messages to all or individual clients
• Ability to add clients to groups
• Messages can be sent to groups
• Handles connection lifetime events
SignalR
19
jQuery Proxy
• Two approaches:
• with generated proxy• Use signalr.exe tool to create the proxy file.
• Without proxy but ‘late binding’• Can be accessed by navigating to http://yoursite/signalr/hubs
• The proxy simplifies the code needed to communicate with the server
SignalR
21
Installing NuGet PackagesGetting Started
Search for SignalRInstall Microsoft ASP.NET SignalR
The required scripts & assemblies
will be added to the project.
22
Route MappingGetting Started
Defined in Global.asax
Defines the routes the client will use to connect to the hub
Part of System.Web.Routing
23
Hub ClassGetting Started
Inherits from Microsoft.AspNet.SignalR.Hub
Sends the message to all the clients bycalling the clients broadcastMessage method
Connected clients can call the Hubs NewMessage method
24
Client (JavaScript)
• Add reference to JavaScript files
• Get reference to Hub
• Wireup events
• Start hub connection
• Call hub methods
Getting Started
27
GroupsHubs API
• Provides the ability to broadcast messages to a selected set of clients
• Groups are not persisted on the server
• No API to retrieve group count or members
The developer would need to
keep track of this (e.g.
database).
Adding a client to a group
Sending
messages
to a group
29
• Override the event handlers
• OnConnected
• OnReconnected
• OnDisconnected
• JavaScript methods available to notify client about connectivity states
• $.connection.hub.connectionSlow
• $.connection.hub.reconnecting
• $.connection.hub.reconnected
• $.connection.hub.disconnected
Lifetime/Lifecycle EventsHubs API
30
• Send data to connected clients by calling methods from outside the Hub
HubContextHubs API
SignalR Client SignalR ServerWindows
Service/Scheduler
The Hub classMethod belongs to a class that is not a Hub
Call the client method
32
• Authorize - specify which users or roles have access to a hub or method
• Scaleout
• Windows Azure Service Bus
• Redis
• SQL Server
Other FeaturesHubs API
34
• Create a ASP.NET Web Application
• Install SignalR using nuget
• Create the Hub
• Update Global.asax
• Publish
• Set web application pool to use .Net Framework 4.0 (you might need to run the aspnet_regiis after changing the framework)
• Optional:
• Set the port to 80 and provide a host header (make sure to update the host file with the same header name)
• Navigate to the site and ensure you can access the JavaScript hub proxy (i.e. http://signalr.mydomain.com/signalr/hubs)
Hub SetupSignalR & SharePoint
35
• Within the page/CEWP/Webpart,etc.. Add reference to the scripts in the hubs site
• Set jQuery to support cross domain communication
• Set the hub url
• Start the connection
Client Setup (SharePoint 2010)SignalR & SharePoint
38
Summary
• Provides the ability to add real time web functionality to applications
• Push data to clients and/or groups
• Hubs enables remote procedure calls (RPCs) to be made from a server to connected clients and from clients to the server
• Not limited to web clients
39
Resources
• SignalR documentation - http://www.asp.net/signalr
• Pluralsight training -http://pluralsight.com/training/Player?author=christian-weyer&name=signalr-introduction-m2-realtime&mode=live&clip=0&course=signalr-introduction
www.virtusa.com
© 2012 All rights reserved. Virtusa and all other related logos are either registered trademarks or trademarks of Virtusa Corporation in the United States, the European Union, and/or India. All other company and service names are the property of their respective holders and may be registered trademarks or trademarks in the United States and/or other countries.
top related