Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

SignalR and Web Sockets

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft ASP.net] [www.ASP.net] [Visual Studio 2012 & One ASP.NET - Scott Hanselman, Principal Community Architect, Microsoft Corporation] Hi, this is Scott Hanselman, and I wanted to talk to you today about Visual Studio 2012, some of the new features in ASP.NET 4.5—in this video SignalR and WebSockets. [SignalR and WebSockets - Scott Hanselman, Principal Program Manager, Microsoft Corporation] SignalR is soon to become part of the ASP.NET family, and it's what you'll use to create real-time services using ASP.NET and WebSocket support in .NET 4.5, and Windows 8 is one of the underpinnings that makes it all come together. So what is SignalR? Well, it is an abstraction over some of the transports that are required to do real time. When you do real-time work, you're going to be starting out over HTTP, but if you have support for WebSockets, you'll promote that connection to a full WebSockets connection. We'll talk about that in a minute and I'll show you a demo. SignalR handles all the connection management for you. It lets you broadcast changes to all the different connected clients like a chat room, or you can target specific clients. And the connection from the client to the server is persistent. It's not like HTTP where you make a call, you tear the connection down, and you start it up again. The SignalR layer of abstraction makes it look like it's persistent. And WebSockets connections are truly persistent. SignalR will automatically negotiate transport. So it will do a fallback. It will see if you've got WebSocket support. And if you don't, it will fall back to other techniques like Server Sent Events, Forever Frames, and, as a last resort, Long Polling. It lets you do server-to-client push. That means that the C# or VB code on the server can call back from server to client and push information out to modern browsers. It scales out to thousands of connections. You can use Service Bus, SQL Server, there's even an open source Redis system for scaling out, and it's all open source on GitHub. The idea is that right now when the client calls the server it just says, "Do you have data? Do you have data? Do you have data?" That's polling and that's no fun. What we want it to do, we want the client browser to say, "Hey, do you do WebSockets?" "Yeah, I do. I do WebSockets." "All right. Let's make a connection." And then we'll do that connection over WebSockets. Let's take a look at what that looks like. The WebSocket support in .NET 4.5 is inside of System.Net.WebSockets. Here's an example of some of the support there is for this low-level connection in ASP.NET. We're using this on IHttpHandler, and there is a new value sitting on HttpContext. So when I'm processing a request at the low level I could say, "Is this a WebSockets request?" "I'm going to accept that." And we pass in a func that takes an ASP.NET WebSocket context, or we handle our response the usual way—with HTTP. Then once we get in here, here is our WebSocket connection request. This is very low-level asynchronous code, and we can really start talking. We can go and open up the WebSocket connection, get the results from the WebSocketReceiveResult and read that message. This is all very low level. But you have that support, so if you're used to writing HTTP handlers and you want to use WebSockets, you can do that. All that support is there, built in to .NET 4.5. Or we can use SignalR. Here I'm bringing in the SignalR.Hubs namespace. I've got a Chat application. This Chat application is deriving from Hub, which is the highest level of abstraction. That Hub has a message. In this case it's called Send. It could be called anything you want. We're calling out to Clients. Notice that Clients is a dynamic. That means I can have anything I want here, any message I want—foomessage. In this case it's addMessage, but Send can be anything and addMessage can be anything. Now if we go over to the client side, take a look at this code side by side, things start to get really interesting. We bring in a script that's dynamically generated called signalr/hubs. SignalR will automatically generate the JavaScript that will let us call chat.send. In JavaScript from the client we connect to chat, and when we click on a button, we'll go and we'll send. Then from the server side when we receive that message we broadcast to all the clients addMessage and then all the clients do. Let's see that live. Let's bring up the browser. I'll bring up 2 windows to pretend that there's 2 different machines. You see that we're sending traffic back and forth, and that information is synchronized between these instances of browsers. So what's happening under the covers? Let's take a look at this Stock Ticker example. I'm going to bring this one up and I'm going to use Fiddler to debug this traffic. Let's hit Reset. What's happening here? Here we're saying we want to use Forever Frame. Forever Frame is not ideal. I'd rather use WebSockets. Let's make sure that our application is compiled using .NET 4.5. We'll go in here and make sure that our httpRuntime and compilation are both .NET 4.5. We'll start our application again. We'll bring up Fiddler, reload our application. Now we're running under .NET 4.5. You'll see that there's a negotiation that happens where we're asking, "Do you support WebSockets?" The server does because we're running Windows 8, and you notice the little socket here that appears in Fiddler. We've just connected with a WebSockets transport. Now I'll open the market and I see the Live Stock Ticker happens. We've got dynamic messages in real time being pushed back from server to client. Let's bring up another browser. You'll notice that traffic is immediately synchronized with the other browser. Both of these browsers are receiving information from the server in real time over WebSockets. WebSockets is a new feature of .NET 4.5. There's support baked in for it at the lowest levels like IHttpHandler in ASP.NET 4.5, and there's also high-level constructs like hubs in SignalR. Whichever one you choose, you're sure to be really productive doing real-time work in ASP.NET. [Microsoft ASP.net] [www.ASP.net]

Video Details

Duration: 6 minutes and 51 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 5
Posted by: neudesicasp on Oct 3, 2013

SignalR and Web Sockets

Caption and Translate

    Sign In/Register for Dotsub to translate this video.