Introduction to SignalR
Introduction to SignalR
By Patrick · Fletcher |2013 in February 27th
The original English text of the address: http://www.asp.net/signalr/overview/getting-started/introduction-to-signalr
This article describes what is SignalR, and it aims to create solutions.
What is SignalR?
ASP.NET SignalR is a library for ASP.NET developers, developers can simplify the real-time Web functionality to process applications. Real time Web function is a function: when the connected clients become server code is available immediately to the push content, rather than wait for a client request new data server.
SignalR can be used for any type of “ real-time ” Web functionality to your ASP.NET applications. Although we often chat application as an example of the most common, but you can use it to do a lot of things. If the user is through to refresh the web page, to see the new data, or through the pages to achieve long polling to retrieve new data, you should consider using the SignalR. Examples include dashboards and monitoring applications, collaborative applications (such as edit documents simultaneously), work schedule updates and real-time form etc.
SignalR also applies to new types of Web applications, especially the application needs to update the server with high frequency, such as real time game. A good example, please refer to the ShootR game.
SignalR provides a simple API used to create the server to the client of the remote procedure call (RPC), to call the client browser from the server.NET code (and other client platform) in the JavaScript function. SignalR also includes a management connection (for example, connection and disconnection events) and connection group API.
SignalR will automatically manage the connection, and allows you to like chat rooms as to all the connected clients and send a message. You can also send a message to a specific client. The connection between the client and the server is persistent, unlike traditional HTTP connected to the — — each of the communication are needed to establish a connection.
SignalR support “ server push ” function, namely the server code can use remote procedure call (PRC) to invoke the browser in the client code, instead of using the commonly used on the Web request response model.
SignalR applications can use the service bus, SQL Server or Redis extension to thousands of clients.
SignalR is open source, can be accessed through GitHub.
SignalR and WebSocket
SignalR will use the new WebSocket transfer mode where possible, and in time of need. Back to the old way of transmission. Although you can still use WebSocket to write applications, but the use of SignalR means that you have many additional features readily available, and without the need to implement these functions. Most importantly, this means that you can use SignalR to write applications using WebSocket, without having to worry about creating code separately for the old client. SignalR also allows you to not have to worry about the WebSocket update, because changes to SignalR will be continuously updated to support basic transmission mode, to provide a uniform interface to your application to use a different WebSocket version.
Of course, you can create a solution using WebSocket, SignalR provides all the functions themselves may need to write code for you, such as back to the transmission mode of other as well as the revision of your application to update to the WebSocket implementation.
Transmission and back
SignalR is a group of in building between the client and the server real-time functions needed transmission technology used in the abstract. SignalR connection in the first HTTP request, then if WebSocket is available, then upgrade to connect to the WebSocket. WebSocket is the ideal transmission mode of SignalR, because it can be the most efficient use of server memory, have the lowest latency, and some main function (such as between the client and server are full duplex communication), but it also has the most stringent environmental requirements: WebSocket server is Windows Server 2012 or Windows 8 and.NET Framework 4.5. If you do not meet these requirements, SignalR will try to establish a connection with other transmission modes.
HTML 5 transmission
Transmission depends on whether to support the HTML 5. If the client browser does not support the HTML 5 standard, will use the transmission mode of older.
- WebSocket(If the server and the browser indicates they support Websocket). WebSocket is the only one built transmission lasting two-way connection between the client and the server in real. However, WebSocket also has the most stringent requirements; only the latest version of Microsoft Internet Explorer, Google Chrome and Mozilla Firefox fully support, other browsers such as Opera and Safari only partial implementation.
- Send the event server , also known as EventSource (if the browser supports sending events, server basically except Internet Explorer other browsers support this function).
Comet transmission
The transmission of Comet based on the Web application model, a browser or other client maintains a long time HTTP requests in this model, the server in the client did not explicitly request using this request to push data to the client.
- Forever Frame(Only Internet Explorer). Forever Frame creates a hidden IFrame, the sending server endpoints on an incomplete request. The server then constantly sending scripts to the client, and the immediate execution of these scripts, so as to establish a from the server to the client one-way real-time connections. From the client to the server is connected with different from client to server connection, as a standard HTML request, for each data needed to send will create a new connection.
- Long polling Ajax. Long polling does not create a persistent connection, but through a request to polling the server, the connection remains open until the server sends the response, and then close the connection, and then immediately request a new connection. This may cause some delay in connection reset.
Detailed information about the transmission support the various configurations, see supported platform.
Transmission mode selection process
The following list shows the transmission mode of SignalR decided to use the steps.
-
If the browser is Internet Explorer 8 or earlier, using long polling.
-
If the configuration of the JSONP (i.e. connected when starting jsonp parameter is set to true), use the long polling.
-
If you are using a connection cross domain (i.e. SignalR endpoint and the host page is not in the same domain), and meet the following conditions will use WebSocket :
-
Client support CORS (Cross-Origin Resource Sharing). Detailed information on what kind of client support CORS, please see the caniuse.com CORS.
-
WebSocket client support
-
The server supports WebSocket
If any one of these standards do not meet, will use the long polling. The detailed information of cross domain connection, see how to build connections across domains.
-
-
If it is not configured to use the JSONP, the connection is not cross domain and the client and server support, will use the WebSocket .
-
If the client or the server does not support the WebSocket, then try to send the event using the server.
-
If the sending event server is not available, it will attempt to use Forever Frame.
-
If Forever Frame is not available, use long polling.
Monitoring transmission mode
You can enable logging in your hub application, and in your browser console window to view your application uses which transport protocol.
Add the following commands to your client application program, to enable hub in your browser event log:
$.connection.myHub.logging = true;
-
In IE, by pressing the F12 key, you can open the developer tools, and then click the console tab.
-
In Chrome, press Ctrl + Shift + J to open the console.
Through the console log records, you can see the SignalR protocol being used.
The specified protocol
Transmission protocol used fixed requires a certain amount of time and the client and server resources. If the client environment is known, then when start the client connection can specify transmission protocol. The following code snippet demonstrates how the known client does not support any other agreement, long polling directly using Ajax at the beginning of a connection.:
connection.start({ transport: 'longPolling' });
If you want a client in a specific order to transmission mode, you can specify to order. The following code snippet demonstrates how to use WebSocket and the failure time to direct the use of long polling.
connection.start({ transport: ['webSockets','longPolling'] });
Used to define the specified string constant transmission mode.:
-
webSockets
-
forverFrame
-
serverSentEvents
-
longPolling
The connection and Hubs
SignalR API contains two kinds of communication between client and server model: permanent connection and Hubs.
Connection is represented as a single group or broadcast message send, simple end point. Developers can use the persistent connection API (represented by the PersistentConnection class in the.NET code) have direct access to the underlying communication protocol SignalR open. Use the connection Api such as WCF developers will be more familiar with the communication model based on connection.
Hub is based on the API connection communication pipeline but higher level, it allows a method to directly call each other client and server. SignalR can magically handle cross machine scheduling, the client can call methods on the server just as easily call native methods, and vice versa. Use the remote call of Api such as.NET Remoting developers will be more familiar with the communication model based on Hubs. Using Hub also allows you to place strong type is passed to a method and binding model.
System structure diagram
The following diagram shows the Hub, persistent connections and for the relationship between the basic transmission technology.
The working principle of Hub
When a method call on the client server code, name and parameters of the server sends a data packet which contains the method to invoke the (when parameters of transmitting method includes the object, it will be serialized as an JSON). Then, the client through the method of method name defined in your client code to match. If the match succeeds, it will execute the client and use the parameter data deserialization.
Tool monitoring methods can be used such as Fiddler calls. The figure below shows the log pane of Fiddler from the SignalR server to the web browser client calls. Method of initiating a call from a Hub called MoveShapeHub, the called method is updateShape.
In this example, the hub name using the parameter identifier H; method name used to identify the parameters of M, and is the method used to send data parameter identifier A. Application form for this message is created in the High-Frequency Realtime tutorial.
The communication model
Most applications should use Hub API. Connect the API can be used for the following:
- The actual message sent to the specified format.
- Developers are more like using message passing and scheduling model, rather than a remote invocation model.
- The use of SignalR transplantation for existing applications are using the message passing model.
Author: Patrick Fletcher, Patrick Fletcher ASP.NET team a programmer and author, currently working on the SignalR project.
Posted by Abbott at November 17, 2013 - 6:45 AM