Skip to content
davidfowl edited this page Nov 5, 2011 · 32 revisions

SignalR JavaScript Client (Persistent Connections)

The SignalR javascript client comes in the form of a jQuery plugin.

Setup

Include the following scripts on your page:

<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>

NOTE: In an MVC application use Url.Content to reference scripts.

Programming Model

$.connection( url )

Creates a new connection.

  • Returns a connection.
  • NOTE: There is currently no cross domain support

Example

var connection = $.connection('/echo');

connection.received( handler(data) )

  • handler(data) - A function to execute each time data is received.

  • Returns a connection.

  • NOTE: data is a JSON object

Example

connection.received(function(data) {
    console.log(data);
});

connection.error( handler(error) )

  • handler(error) - A function to execute each time an error occurs.

  • Returns a connection.

Example

connection.error(function(error) {
    console.warn(error);
});

connection.sending( handler )

  • handler - A function to execute before the transport establishes a connection to the server.

connection.start( )

Starts the connection using the default settings.

  • NOTE: A connection cannot send or receive messages until the start method has been called and the connection successfully established.

connection.start( callback )

Starts the connection using the default settings and executes the passed callback once the connection has been established.

connection.start( settings )

Starts the connection using the passed settings.

connection.send( data )

  • data - data to send over the connection.

Example

connection.send("Hello World");

Clone this wiki locally