signalr - realtime client/server communication (isense)
TRANSCRIPT
SignalRRealtime client/server communicationMaarten Balliauw@maartenballiauw
R
Who am I?
Maarten Balliauw
Antwerp, Belgium
Developer Advocate, JetBrains
Founder, MyGet
AZUG
Focus on webASP.NET MVC, Azure, SignalR, ...Former MVP Azure & ASPInsider
Big passion: Azure
http://blog.maartenballiauw.be
@maartenballiauw
Agenda
Why real-time & how?
Meet SignalR
Connections and Hubs
Clients
.NET Core
Q&A
Why real-time & how?
Users want the latest info, now!
Twitter – live searches/updates
Stock tickers
Auctions
Live scores
Real-time notifications
Collaborative apps
Live user analytics
Online gaming / browser games
… 6
HTTP is and old beast
Never designed for real-time communications
Web is request-response
Web is stateless
Websockets to the rescue!
Websockets
Extension to HTTP
Provide raw sockets over HTTP
Full-duplex, low latency
Traverses proxies
But…Not every proxy server supports itNot every webserver supports itNot every browser supports itSome antivirus blocks itThey are raw sockets! (protocol: up to you)
http://websocketstest.com/
Forever Frame
Server tells client that response is chuncked
Client keeps connection open untill server closes it
Server pushes data to the client followed by \0
Consumes server threads
HTTP/1.1 200 OKContent-Type: text/plainTransfer-Encoding: chunked
<script>eval("... ")</script>\0
<script>eval("... ")</script>\0
Periodic polling
Poll from time to time using Ajax
Delay in communications due to polling interval
Wastes bandwidth & latency
Polling interval
Long polling
Poll but don’t respond until there’s data
Poll again after data received or after the connection times out
Consumes server threads & connection resources
Options!Forever Frame
Periodic polling
Long polling
Websockets
(Server-Sent events)
SignalR
SignalR
Three-in-one!“Persistent” client/server connection over best transport
Connection negotiation
Abstracts away the transportProvides just one programming model
http://github.com/signalr/signalr - open sourceC#, JavaScript, UWP
https://github.com/aspnet/signalr - new .NET Core version
Hello, SignalRDEMO
What just happened?
The server is broadcasting a message every few seconds
Clients are receiving messages
Code looks easy
No polling or whatsoever (at least not in my code)
SignalR decides on transport mechanism used based on client + server
Connections and Hubs
Two programming models
PERSISTENT CONNECTION
Can communicate with 1..N clients
Requires a route to be defined
Limited to sending messages
You define the “protocol”
HUB
Abstraction over PersistentConnection
Can communicate with 1..N clients
Route automatically mapped (/signalr/hubs)
Can send messages and call methods
SignalR defines the protocol
Hello, HubsDEMO
Hubs
Hub methods can be called from client
Client methods can be called from hub
Target individual client
Target all clients
Target group of clients
http://shootr.signalr.net/
Clients
So far we have used...
On the server side
Host in any ASP.NET application (SignalR.Server)
On the client side
JavaScript (SignalR.JS)
But there’s more…
Connecting to SignalR DEMO
There is more!
On the server side
Host in any ASP.NET application (SignalR.Server) or using self-hosting (OWIN based)
Host on Azure
Scale out (Redis, Azure Service Bus)(I did a NodeJS server as well, but don’t use it in production)
On the client side
JavaScript (SignalR.JS), Angular (AngularJs.SignalR.Hub)
Any .NET client (SignalR.Client)
UWP
iOS
Android
DeckCastDEMO
.NET Core
ASP.NET Core Sockets
SignalR in the ASP.NET Core world
Improvements based on SignalR learnings:
No more jQuery
No more iframe transport
No more message replay after reconnect (memory usage, performance)
No more Hub-state (I did not talk about this “ViewState” because it’s evil)
Endpoint per Hub (/signalr/hubs /signalr/...)
Scale-out now becomes scale-out (and not sending all messages everywhere)
Sticky sessions required (sad )
ASP.NET Core Sockets
New features:
Cross-platform (including things like RaspberryPi)
Binary data support (think sending images etc.)
Host-agnostic (HTTP works but also TCP/UDP/... sockets)
Protocol data format pluggable (JSON, protobuf, ...)
TypeScript client
...
Demo?
Will leave this one for you...
Under development! https://github.com/aspnet/signalr
Preview release mid-year
Release later this year
Conclusion
Conclusion
SignalR is three-in-one!
“Persistent” client/server connection over best transport
Abstracts away the transport
Provides just one programming model
Connections & Hubs
Connect various clients
Thank you!http://blog.maartenballiauw.be
@maartenballiauw