ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
1
SignalR
il WebSocket che tanto ci mancava
@cloudgen_verona
#CodeGen
#dotnetconf
3
Main sponsor
4
Community sponsors
5
ATosato86
andreatosato
andrea.tosato
ANDREA TOSATO
7
Storia
Dalla nascita alla ri-nascita
8
I creatori
David Fowler Damian Edwards
SignalR nacque nel 2011
Socket.io, release (0.1.0), il 19/03/2010
9
La storia in ASP .NET
Fu portato nel progetto ASP.NET nel 2013.
In quell’anno WebSocket era un protocollo, appena
standardizzato, ma che molti browser non conoscevano.
Si utilizzavano tecnichiche come polling e altre tecniche
come il Server-Side Event non erano ancora
completamente implementate dai browser.
SignalR nacque per risolvere il problema e inserire un
supporto all’interno dello stack ASP .NET. SignalR divenne
una libreria di riferimento per il RealTime.
10
La storia in ASP .NET
SignalR quindi si fa carico di negoziare il miglior
protocollo di trasporto disponibile per la comunicazione.
SignalR, alla nascita, era strettamente accoppiato a jQuery.
Il web di allora lo era quasi interamente.
Altre funzionalità native dello strumento:
• Riconnessione automatica
• Scalabilità chiavi in mano (Service-Bus)
11
La rinascita
12
La rinascita
Nel 2018 viene interamente riscritto.
Nessuna dipendenza con la libreria jQuery.
Scalabilità (Redis – Azure SignalR Service)
Cross-platform
Estendibile
Disponibile dalla versione ASP .NET Core 2.1
Client: Javascript – Typescript – .NET - Node.js - Python
Prossime release: Java e C++
Planning: Go, PHP
13
I pacchetti disponibili per ASP .NET Signlar
ASP .NET:
https://www.nuget.org/packages/Microsoft.AspNet.SignalR/
JavaScript:
https://www.nuget.org/packages/Microsoft.AspNet.SignalR.JS/
C++: https://github.com/SignalR/SignalR-Client-Cpp
NodeJS: None
PHP: None
Go: None
Python: https://pypi.org/project/signalr-client/
Java: https://github.com/SignalR/java-client
14
I pacchetti disponibili per ASP .NET Core
ASP .NET Core: 'Microsoft.AspNetCore.SignalR.Client' on NuGet
(https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR/)
Java: 'com.microsoft.aspnet:signalr' on Maven
(https://search.maven.org/artifact/com.microsoft.aspnet/signalr)
JavaScript (including NodeJS): '@aspnet/signalr' on NPM
(https://www.npmjs.com/package/@aspnet/signalr)
C++: (Prototype) https://github.com/aspnet/SignalR/tree/master/clients/cpp
(no official release planned yet)
Swift: (Unofficial) SwiftSignalRClient on CocoaPods
(https://github.com/moozzyk/SignalR-Client-Swift)
PHP ?
Go: Planning
Python: Planning
https://github.com/aspnet/SignalR/tree/release/2.2/specs Per contribuire
15
Protocolli di
trasporto
Browser supportati, scenari legacy
“A transport is required to have
the following attributes:
Duplex
Binary-safe
Text-safe
17
SignalR
A transport is required to have the following attributes:
Duplex
Able to send messages from Server to Client and from Client
to Server
18
SignalR
A transport is required to have the following attributes:
Binary-safe
Able to transmit arbitrary binary data, regardless of content
19
SignalR
A transport is required to have the following attributes:
Text-safe
Able to transmit arbitrary text data, preserving the content.
Line-endings must be preserved but may be converted to a
different format. For example rn may be converted to n.
This is due to quirks in some transports (Server Sent Events). If
the exact line-ending needs to be preserved, the data should
be sent as a Binary message.
20
SignalR
The only transport which fully implements the duplex
requirement is WebSockets
The others are "half-transports" which implement one end of
the duplex connection.
They are used in combination to achieve a duplex connection.
21
SignalR
ASP.NET Core SignalR was built to be a general purpose
RPC(remote procedure call) based communication library.
It supports:
• Multicast
• Broadcast
• Groups
• Bidirectional RPC
• Streaming
22
Long Polling
Richieste continue Client-Server
Client
Request
Response
Request
Response
Request
Wait
23
Server-Sent Events
Connessione uniderezionale tra Server e Client
Client
Open Connection
Events
Close Connection
24
Server-Sent Events
25
Server-Sent Events
26
Avidità dei protocolli
27
Web Socket
WebSocket è una tecnologia web che fornisce canali di
comunicazione full-duplex attraverso una singola
connessione TCP. L'API del WebSocket è stata
standardizzata dal W3C e il protocollo WebSocket è stato
standardizzato dall'IETF come RFC 6455.
WebSocket è disegnato per essere implementato sia lato
browser che lato server, ma può essere utilizzato anche da
qualsiasi applicazione client-server.
Fonte: https://it.wikipedia.org/wiki/WebSocket
28
Web Socket
29
Web Socket
30
Web Socket
31
Forever frame
32
Protocolli di
messaggio
Json, msgpack, custom
33
JSON
34
JSON
{
"arguments":[
{
"To":{
"Username":"demo",
"ConnectionId":"cqvsDNFlhjht7c6-NyWgvQ"
},
"From":{
"Username":"andrea.tosato",
"ConnectionId":"JLn11llArunDqCMmVqFrbg"
},
"TextMessage":"Buongiorno e benvenuto"
}
],
"target":"AddPrivateMessage",
"type":1
}
35
MessagePack
https://msgpack.org/
36
Message Pack vs Json
37
MessagePack
Microsoft.AspNetCore.SignalR.Protocols.MessagePack
Client
@aspnet/signalr-protocol-msgpack
<script src=/slideshow/signalr-il-websocket-che-tanto-ci-mancava/116097996/"~/lib/signalr/signalr.js"></script>
<script src="~/lib/msgpack5/msgpack5.js"></script>
<script src="~/lib/signalr/signalr-protocol-msgpack.js"></script>
“If you want to implement a
custom message protocol,
ASP.NET Core SignalR has
extensibility points that allow
new protocols to be plugged in
Demo
Heart Rate
40
Connessione e
trasferimento dati
Negoziazione della connessione, invio e
ricezione dati
41
Negotiate
{
"connectionId":"nw-Mk2QvXGxS5WyffMao4A",
"availableTransports":[
{
"transport":"WebSockets",
"transferFormats":["Text", "Binary"]
},
{
"transport":"ServerSentEvents",
"transferFormats":["Text"]
},
{
"transport":"LongPolling",
"transferFormats":["Text", "Binary"]
}
]
}
42
Streaming
ASP.NET Core SignalR supporta streaming valori restituiti
dei metodi del server.
Ciò è utile per scenari in cui verranno inviati frammenti di
dati nel corso del tempo.
Quando un valore restituito viene trasmesso al client, non
appena diventa disponibile, anziché attendere che tutti i dati
diventino disponibili.
43
Streaming – oggetti da utilizzare
ChannelReader<T>
Restituisce un valore non appena disponibile.
ChannelWriter<T>
Consente la scrittura di dati all’interno di uno stream.
Demo
Streaming
45
Streaming – Cooming soon
Server To Client (Work)
Client To Server (Cooming soon)
46
Scalare SignalR
Scenari On-Premise
47
Perchè Scalare?
Server A - IIS
WebApp
Client 1 Client 2 Client 3 Client N
48
Load Balancer
Perchè Scalare?
Server B - IIS
WebApp
Client 1 Client 2 Client 3 Client N
Server A - IIS
WebApp
49
Load Balancer
Perchè Scalare?
Server B - IIS
WebApp
Client 1 Client 2 Client 3 Client N
Server A - IIS
WebApp
Backplane
50
Backplane
Redis - Pub/Sub
https://redis.io/topics/pubsub
51
Backplane
Sticky Sessions
52
Backplane
Round Robin
Round Robin
with Sticky Session
Server A Server B Server A Server B
User 1 User 2 User 1 User 2
53
Backplane
services
.AddSignalR()
.AddRedis("ConnectionString");
Microsoft.AspNetCore.SignalR.Redis
54
Scalare SignalR
Scenari Cloud
55
Azure SignalR Services
SignalR è un servizio completamente gestito.
puoi implementarlo in un ambiente multiserver senza
preoccuparti di:
• hosting,
• scalabilità,
• bilanciamento del carico
• autenticazione.
56
API REST
SWAGGER
https://editor.swagger.io/
SWAGGER DOC
https://github.com/Azure/azure-
signalr/blob/dev/docs/swagger.json
Demo
Azure Signalr Service
58
Azure SignalR Service
ASP .NET versione 2.4
Supporto Azure SignalR per scalabilità chiavi in mano
Coming in 2019
59
Serverless
Scenario Serverless
60
SignalR - Serverless
Grazie a una estensione per Azure Functions non è
necessario avere la parte server di SignalR su una
applicazione ASP .NET Core.
L’estensione ha grossi limiti, ma per alcuni sceri può essere
utile.
https://github.com/anthonychu/AzureAdvocates.WebJobs.Extensions.SignalRService
Current limitations
•Only supports broadcasting at this time, cannot invoke methods on a subset of connections,
users, or groups
•Functions cannot be triggered by client invocation of server methods (clients need to call an
HTTP endpoint or post messages to a Event Grid, etc, to trigger a function)
Demo
Serverless - Desktop
62
Esempio completo
Creare una chat con SignalR
Demo
Chat
Grazie
Domande?
andreatosato ATosato86 Andrea.Tosato

More Related Content

SignalR, il WebSocket che tanto ci mancava

Editor's Notes

  1. Forever Frame creates a hidden IFrame which makes a request to an endpoint on the server that does not complete. The server then continually sends script to the client which is immediately executed, providing a one-way realtime connection from server to client. The connection from client to server uses a separate connection from the server to client connection, and like a standard HTTP request, a new connection is created for each piece of data that needs to be sent.
  2. Crea JWT Token: http://jwtbuilder.jamiekurtz.com/ Port REST APIs are only exposed on port 5002. Authentication In each HTTP request, an authorization header with a JSON Web Token (JWT) is required to authenticate with Azure SignalR Service. Signing Algorithm and Signature HS256, namely HMAC-SHA256, is used as the signing algorithm. You should use the AccessKey in Azure SignalR Service instance's connection string to sign the generated JWT token. Claims aud (audience) and exp(expiration time) are required claims in the JWT token. The aud claim should be exactly the same as your HTTP request url, trailing slash and query paramters not included. For example, a broadcast request's audience should look like as below: https://example.service.signalr.net:5002/api/v1-preview/hub/myhub Esempio: https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/Serverless
  3. Portale Scalabilita (Up – Out) ASP .NET Core Secret Startup progetto
  4. Portale Scalabilita (Up – Out) ASP .NET Core Secret Startup progetto