SELA DEVELOPER PRACTICEMay 5-9, 2013Ran WahleReal time Web experience with signalR
Agenda• “Real Time” web techniques• What is signalR• SignalR protocol• Hubs• Groups and private messages• .Net client• Sel...
Real-time webPollingForeverFramesLong-PollingServerSentEventsWebSocket
Polling
Long Polling
Server Sent Events
Forever Frames
Web SocketWeb Socket RequestTCP SocketServerClient
Web socket• Actual TCP socket between server and client• HTML5 standard• Supported by Windows server 2012 / Windows 8• Sup...
Web socket in codeServer (C#):public void ProcessRequest(HttpContext context){HttpContext.Current.AcceptWebSocketRequest(n...
Web socket in codeServer (C#)public class MyWebSocketHandler :WebSocketHandlerpublic override void OnOpen()public override...
WebSocket is not enough• Not all servers support web sockets• Not all network components support web sockets• It’s API is ...
What do we need?• Code once, deploy everywhere• As cross-platform as possible• Simple API to work with
What is signalR• Open source project• Abstraction layer over connection technique• Can be self-hosted• Call client methods...
So, why not WebSocketWebSocket• Good performance• Strict prerequisites• Limited APISignalR• Abstraction layer• Rich API• S...
Getting started“Every time I see live-coding I get down on my kneesand pray”Anonymous narrator
ProtocolNegotiate requestNegotiate responseConnection phaseClientServer
Try it yourselfhttp://ranw.azurewebsites.net/chatpage.html
Hub• Abstraction over signalR connection• Service-like API• Call clients methods from the server
HubServer (C#)public void ServerMethod(args){…..}private void SendToClient(){Clients.All.doSomething(args);}Client (Javasc...
Private messages• You can use connection ID• You can access a particular client by itClients.Client(connectionId).sendMess...
Groups• Part of Hub API• Stored on client• Not persistentGroups.Add(Context.ConnectionId, groupName);Clients.Group(groupNa...
.Net client_connection = new HubConnection(“Server Url");IHubProxy proxy = connection.CreateHubProxy(“Hub Name");_connecti...
Self hosting• Three NuGet packages to installMicrosoft.Owin.HostingMicrosoft.Owin.Host.HttpListenerMicrosoft.AspNet.Signal...
ScaleOut with Redis• Install Redis for Linux or Clone the windows porting• Add One line of codeGlobalHost.DependencyResolv...
What have we seen today?• Real time experience is more than just polling• Using signalR can use the best available techniq...
Other resources•http://www.asp.net/signalr•http://signalr.net/•Twitters:@signalr @DamianEdwards @davidfowland @DavianFowlw...
Thank YouRan Wahleranw@sela.co.ilhttp://blogs.Microsoft.co.il/blogs/ranw
Upcoming SlideShare
Loading in …5
×

Realtime web experience with signal r

2,943 views

Published on

Real-time web experience with signalR. Slide deck from SDP 2013 session

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,943
On SlideShare
0
From Embeds
0
Number of Embeds
518
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Realtime web experience with signal r

  1. 1. SELA DEVELOPER PRACTICEMay 5-9, 2013Ran WahleReal time Web experience with signalR
  2. 2. Agenda• “Real Time” web techniques• What is signalR• SignalR protocol• Hubs• Groups and private messages• .Net client• Self Hosting• Scaleout
  3. 3. Real-time webPollingForeverFramesLong-PollingServerSentEventsWebSocket
  4. 4. Polling
  5. 5. Long Polling
  6. 6. Server Sent Events
  7. 7. Forever Frames
  8. 8. Web SocketWeb Socket RequestTCP SocketServerClient
  9. 9. Web socket• Actual TCP socket between server and client• HTML5 standard• Supported by Windows server 2012 / Windows 8• Supported by IE10+, Chrome, Firefox, Opera andmore
  10. 10. Web socket in codeServer (C#):public void ProcessRequest(HttpContext context){HttpContext.Current.AcceptWebSocketRequest(new ChatWebSocketHandler());context.Response.StatusCode=(int)HttpStatusCode.SwitchingProtocols;}Client: (javascript)var websocket = new window.WebSocket(“ws://<web socket url>”);
  11. 11. Web socket in codeServer (C#)public class MyWebSocketHandler :WebSocketHandlerpublic override void OnOpen()public override void OnError()public override void OnMessage(stringmessage)Client (Javascript)websocket.onopen = function () { …}websocket.onerror = function(data) {…}websocket.onmessage = function (event) {
  12. 12. WebSocket is not enough• Not all servers support web sockets• Not all network components support web sockets• It’s API is limited
  13. 13. What do we need?• Code once, deploy everywhere• As cross-platform as possible• Simple API to work with
  14. 14. What is signalR• Open source project• Abstraction layer over connection technique• Can be self-hosted• Call client methods from the serverServerSentEvents=PollingForeverFramesLong-PollingWebSocket
  15. 15. So, why not WebSocketWebSocket• Good performance• Strict prerequisites• Limited APISignalR• Abstraction layer• Rich API• Supports WebSocket
  16. 16. Getting started“Every time I see live-coding I get down on my kneesand pray”Anonymous narrator
  17. 17. ProtocolNegotiate requestNegotiate responseConnection phaseClientServer
  18. 18. Try it yourselfhttp://ranw.azurewebsites.net/chatpage.html
  19. 19. Hub• Abstraction over signalR connection• Service-like API• Call clients methods from the server
  20. 20. HubServer (C#)public void ServerMethod(args){…..}private void SendToClient(){Clients.All.doSomething(args);}Client (Javascript)hub.server.serverMethod(args);hub.client.doSomething = function(args){…};
  21. 21. Private messages• You can use connection ID• You can access a particular client by itClients.Client(connectionId).sendMessage(message)
  22. 22. Groups• Part of Hub API• Stored on client• Not persistentGroups.Add(Context.ConnectionId, groupName);Clients.Group(groupName).invokeSomeClientMethod(args…);
  23. 23. .Net client_connection = new HubConnection(“Server Url");IHubProxy proxy = connection.CreateHubProxy(“Hub Name");_connection.Start().ContinueWith(//define proxies);//invoke server methods_proxy.Invoke(“HubServerMethod“ , args…));
  24. 24. Self hosting• Three NuGet packages to installMicrosoft.Owin.HostingMicrosoft.Owin.Host.HttpListenerMicrosoft.AspNet.SignalR.Owin• Create class with Configuration method.• Map hub in the Configuration method• Use the class as a type parameter toWebApplication.Start
  25. 25. ScaleOut with Redis• Install Redis for Linux or Clone the windows porting• Add One line of codeGlobalHost.DependencyResolver.UseRedis(“<server>", <port>, “<password>", “<storagekey>");• On Windows – run redis from command line• Your app is ready to scaleout
  26. 26. What have we seen today?• Real time experience is more than just polling• Using signalR can use the best available technique• It can be self hosted• It can scale out• It has several clients
  27. 27. Other resources•http://www.asp.net/signalr•http://signalr.net/•Twitters:@signalr @DamianEdwards @davidfowland @DavianFowlwards•http://blogs.Microsoft.co.il/blogs/ranw
  28. 28. Thank YouRan Wahleranw@sela.co.ilhttp://blogs.Microsoft.co.il/blogs/ranw

×