![Page 1: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/1.jpg)
SignalRRealtime client/server communicationMaarten Balliauw@maartenballiauw
![Page 2: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/2.jpg)
R
![Page 3: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/3.jpg)
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
![Page 4: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/4.jpg)
Agenda
Why real-time & how?
Meet SignalR
Connections and Hubs
Clients
.NET Core
Q&A
![Page 5: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/5.jpg)
Why real-time & how?
![Page 6: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/6.jpg)
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
![Page 7: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/7.jpg)
HTTP is and old beast
Never designed for real-time communications
Web is request-response
Web is stateless
Websockets to the rescue!
![Page 8: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/8.jpg)
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/
![Page 9: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/9.jpg)
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
![Page 10: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/10.jpg)
Periodic polling
Poll from time to time using Ajax
Delay in communications due to polling interval
Wastes bandwidth & latency
Polling interval
![Page 11: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/11.jpg)
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
![Page 12: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/12.jpg)
Options!Forever Frame
Periodic polling
Long polling
Websockets
(Server-Sent events)
![Page 13: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/13.jpg)
SignalR
![Page 14: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/14.jpg)
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
![Page 15: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/15.jpg)
Hello, SignalRDEMO
![Page 16: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/16.jpg)
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
![Page 17: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/17.jpg)
Connections and Hubs
![Page 18: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/18.jpg)
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
![Page 19: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/19.jpg)
Hello, HubsDEMO
![Page 20: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/20.jpg)
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/
![Page 21: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/21.jpg)
Clients
![Page 22: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/22.jpg)
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…
![Page 23: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/23.jpg)
Connecting to SignalR DEMO
![Page 24: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/24.jpg)
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
![Page 25: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/25.jpg)
DeckCastDEMO
![Page 26: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/26.jpg)
.NET Core
![Page 27: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/27.jpg)
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 )
![Page 28: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/28.jpg)
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
...
![Page 29: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/29.jpg)
Demo?
Will leave this one for you...
Under development! https://github.com/aspnet/signalr
Preview release mid-year
Release later this year
![Page 30: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/30.jpg)
Conclusion
![Page 31: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/31.jpg)
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
![Page 32: SignalR - Realtime client/server communication (iSense)](https://reader034.vdocuments.net/reader034/viewer/2022051123/5a6525017f8b9a587a8b45f1/html5/thumbnails/32.jpg)
Thank you!http://blog.maartenballiauw.be
@maartenballiauw