PPTX - PhilKnows.Net

linencharmMobile - Wireless

Dec 10, 2013 (3 years and 7 months ago)

69 views

Pushing
realtime

data to client


Phil Denoncourt

philknows.net

SignalR


History


Pushing Data from Server
-
>Client

Browser Request
Servers Response
Anything Happen
?
Nope
Anything Happen
?
Nope
Anything Happen
?
Nope

Library which allows you to push data from ASP.NET server directly to the
client browser


Uses a variety of methods depending on


Network conditions / Firewalls


Browser capability


HTML 5
WebSockets


Server Sent Events


Forever Frame


Ajax Long Polling


Send
async

messages from server to the client, or client to the server


Able to handle connect / disconnect events from the server

Signal R is…


Created July 2011 by David Fowler as moonlighting project


Found that Damian Edwards was working on similar project


So they got together



MIT Licensed, hosted on
GitHub

History


SignalR

project


Notification of server events


Important changes via service calls


Database polling


Pulling from a message queue


Charting of real time data


Collaborative apps


Chat



Possible Uses


Browsers


Desktop IE 7+, Chrome, Safari, Firefox


Mobile/Tablets: Android,
iOS
, Window Mobile


Desktop


.NET (
winForm
, WPF, Silverlight, Mono)


iOS

Native Apps


(3
rd

party)


Android Native Apps


(3
rd

party)


Javascript




It can communicate with:


Install
-
Package
SignalR



Server components written for .NET 4.5


Generally hosted in web app


Websockets

rely on IIS 8


Can be self hosted in windows service, or console

Getting Started


Low level API for sending bits to the client


Class must inherit from
PersistentConnection


Broadcasts an object directly to a set of Clients


Message is JSON serialized


Client must decide what to do with message



Generally, you won’t use this

Development


Persistent Connections


Main abstraction used to develop in
SignalR


Hub represents a set of clients connected to the same “channel”


Create class, inheriting from Hub


Expose methods that clients can call


These are one way methods

NO RESULTS ARE RETURNED TO CLIENT


An instance of the hub is created for each call


Can send messages to clients using the Client dynamic object

Development
-

Hubs


All messaging from server is done via
Clients
property


Clients.All.advanceToSlide
(2);



Sends the
msg

object to all connected clients


Each client is expected to have
javascript

function named
messageSent



Can send to All, Groups, or a specific connection

Development


Sending messages


Events are raised on the server for changes in the connection state



OnConnected

-
> When a client connects for the first time


OnDisconnected

-
> When a client becomes disconnected


Reason is unknown; closing browser, network failure, deliberate closing


OnReconnected

-
> When a client reconnects after having some
disconnection.


If there were network problems..

Development


reacting to lifetime events


Add script reference to /
signalr
/hubs


Loads a dynamically generated script with hub methods



Once connection is opened to hub, client methods can be invoked


For each client
-
side method, you need to add it to the connection


this.slideConnection.client.advanceToSlide

=
function(slide) {


impress().
goto
(
parseInt
(slide));

};

Development
-

Javascript


Use Network feature in Chrome for Web API traffic



Can watch traffic via fiddler


Make sure stream button is hit


Otherwise
SignalR

falls back to long polling

Development
-

Monitoring


Able to change the way
SignalR

does:



Queues / Sends Messages


Locates assemblies that contain Hubs


Generates the client proxy


Minifies the client proxy


Serializes objects to / from JSON



Plug your custom type into
GlobalHost.DependencyResolver

Development
-

Extensibility


Massive amount of
async

allows 1000’s to connect to a single server


Can be run with load balancers


Need to configure to use an alternate messaging store:


SQL Server


Redis


Azure service bus


Bound by allowable connections to IIS

Scaling


Tough to troubleshoot


Why the transport downgrades



API isn’t mature; keeps changing

Problems


@
SignalR


Twitter feed


Signalr.net


Main website


ASP.NET/
SignalR


Microsoft’s content/tutorials


http://
t.co/oHWaZb2a47


E
-
Book by Jose Aguilar


Resources