Come ti "pusho" il web con WebSockets: da 0 a SignalR

Post on 21-Feb-2017

47 views 0 download

Transcript of Come ti "pusho" il web con WebSockets: da 0 a SignalR

18 NOVEMBRE 2016

Come ti "pusho" il web con WebSockets: da 0 a SignalRAlessandro Melchiori / @amelchiori

Web.vNext()

• Today’s applications demand real-time, low-latency messaging•Users expect to be able to interact with data as close

to real-time as possible•No needs to hit refresh button

Periodic Polling

Polling an endpoint at a given interval and returning data when it is available

Long polling

The server/endpoint holds on to the HTTP request until there is data/payload available and then returns to the client. The client then follows up with another request and waits until new data or event/payload is available.

The WebSocket protocol

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g., using XMLHttpRequest or <iframe>s and long polling).

https://tools.ietf.org/html/rfc6455

The WebSocket protocol

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g., using XMLHttpRequest or <iframe>s and long polling).

https://tools.ietf.org/html/rfc6455

WebSocket: how it works

• Client and server establish initial handshake over HTTP/S • Client request upgrades• Server accepts upgrade request and establish socket connection• Client and/or server send messages

DEMO

Welcome SignalR

• SignalR is a library that allows bi-directional communication between server and client. • SignalR supports Web Sockets, and falls back to other compatible

techniques for older browsers. • SignalR includes APIs for connection management (for instance,

connect and disconnect events), grouping connections, and authorization.

SignalR architecture

message cache

publisher

client client client client client

worker worker worker

message bus

- serializzazione- salvataggio in cache- “deliverable”

- selezione worker- recupero messaggio

notifica al client (specific transport)

DEMOInstall-Package Microsoft.AspNet.SignalR

BackendBackend

SignalR: real scenario

FrontEnd

Rabb

itMQ

Backend

MSG

HTTP

MSG

MSGMSG

WS

DEMO

FrontEndFrontEnd

SignalR: scale-out

BackendBackend

FrontEnd

Rabb

itMQ

Backend

MSG

HTTP

MSG

MSGMSG

WS

SignalR: scale-out

•How do messages from one server get to the other servers in my web farm?

SignalR: scale-out

web nodes

clients

backplane

DEMO

SignalR: asp.net core?• Open source: https://github.com/aspnet/SignalR

• Completely rewritten• Client package -> typescript

• Full integration with asp.net core IoC

SignalR: asp.net core?

•Configure nuget to load preview package

<?xml version="1.0" encoding="utf-8"?>

<configuration>

<packageSources>

<add key="AspNetCore" value="https://dotnet.myget.org/F/aspnetcore-ci-dev/api/v3/index.json" />

<add key="NuGet" value="https://api.nuget.org/v3/index.json" />

</packageSources>

</configuration>

Grazie!

Contatti:

• https://twitter.com/amelchiori• http://melkio.codiceplastico.com• alessandro@codiceplastico.com

Thank you! Questions?https://twitter.com/ugidotnet