signalr - realtime client/server communication (isense)

32
SignalR Realtime client/server communication Maarten Balliauw @ maartenballiauw

Upload: maarten-balliauw

Post on 22-Jan-2018

399 views

Category:

Internet


3 download

TRANSCRIPT

Page 1: SignalR - Realtime client/server communication (iSense)

SignalRRealtime client/server communicationMaarten Balliauw@maartenballiauw

Page 2: SignalR - Realtime client/server communication (iSense)

R

Page 3: SignalR - Realtime client/server communication (iSense)

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)

Agenda

Why real-time & how?

Meet SignalR

Connections and Hubs

Clients

.NET Core

Q&A

Page 5: SignalR - Realtime client/server communication (iSense)

Why real-time & how?

Page 6: SignalR - Realtime client/server communication (iSense)

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)

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)

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)

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)

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)

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)

Options!Forever Frame

Periodic polling

Long polling

Websockets

(Server-Sent events)

Page 13: SignalR - Realtime client/server communication (iSense)

SignalR

Page 14: SignalR - Realtime client/server communication (iSense)

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)

Hello, SignalRDEMO

Page 16: SignalR - Realtime client/server communication (iSense)

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)

Connections and Hubs

Page 18: SignalR - Realtime client/server communication (iSense)

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)

Hello, HubsDEMO

Page 20: SignalR - Realtime client/server communication (iSense)

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)

Clients

Page 22: SignalR - Realtime client/server communication (iSense)

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)

Connecting to SignalR DEMO

Page 24: SignalR - Realtime client/server communication (iSense)

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)

DeckCastDEMO

Page 26: SignalR - Realtime client/server communication (iSense)

.NET Core

Page 27: SignalR - Realtime client/server communication (iSense)

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)

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)

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)

Conclusion

Page 31: SignalR - Realtime client/server communication (iSense)

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)

Thank you!http://blog.maartenballiauw.be

@maartenballiauw