Interact with Kaazing WebSocket Gateway Using the EventSource API

This procedure describes how you can use the EventSource API—provided by the Kaazing Silverlight or .NET client library—from your application. This API allows you to take advantage of the server-sent events standard as described in the HTML5 specification. For example, you can create an application that uses the Kaazing Silverlight client library to receive streaming data from a news feed or streaming financial data. The support for server-sent events is provided by the EventSource class and its supporting classes.

The following steps allow you to use the EventSource API in a Silverlight or .NET Framework application. This example highlights some of the most commonly used EventSource methods and is not meant to be an end-to-end tutorial.

Before You Begin

This procedure is part of Checklist: Build Microsoft .NET and Silverlight Clients Using Kaazing WebSocket Gateway:

  1. Set Up Your Development Environment
  2. Interact with Kaazing WebSocket Gateway Using the WebSocket API
  3. Interact with Kaazing WebSocket Gateway Using the EventSource API
  4. Interact with Kaazing WebSocket Gateway Using the ByteSocket API
  5. Configure Automatic Connection
  6. Secure Your Microsoft .NET and Silverlight Client
  7. Display Logs for .NET and Silverlight Clients
  8. Troubleshoot Your Microsoft .NET and Silverlight Clients

Note: Learn about supported browsers, operating systems, and platform versions in the Release Notes.

To Use the EventSource API

  1. Add the required import statement:

    using Kaazing.HTML5;

    Note: The .NET and Silverlight client libraries are signed.

  2. Create a new EventSource object, as shown in the following example.

    EventSource mySource = new EventSource();

  3. Add event listeners to the EventSource object. The following example shows how three event listeners are added to the EventSource object. The open listener is called when a server-sent events connection is established, the message listener is called when messages are received, and the error listener is called when errors occur.
    mySource.OpenEvent += new OpenEventHandler((object sender,
      OpenEventArgs e) =>
      {
        // Update the UI to show that the Silverlight application
        //            is now receiving events.
      });
    
    mySource.MessageEvent += new MessageEventHandler((object sender,
    MessageEventArgs e) =>
      {
        string messageData = e.Data;
        string origin = e.Origin;
        string lastEvent = e.EventId;
    
        // Use the data and event info to update the UI.
        //            For example:
          this.Dispatcher.BeginInvoke(() =>
          {
            Message.TextBox.text = messageData;
          });
      });
    
    mySource.ErrorEvent += new ErrorEventHandler((object sender,
      ErrorEventArgs e) =>
        {
          // Handle errors by closing the application
          //            or attempting to reconnect.
        });

    Note: The Kaazing client libraries fire events on multiple threads for better performance. Therefore, events are delivered on a thread other than the UI thread. This requires you to explicitly transfer execution to the UI thread when updating the UI from dispatched events. This technique varies slightly between Silverlight and .NET Framework applications.

  4. Connect to an event source as shown in the following example. Once this method is called, the EventSource is activated and message can flow through at any time.

    mySource.Connect("http://localhost:8000/sse");

  5. Later, you can call a method to disconnect the EventSource in case you want to stop listening to messages from a particular event source.

    mySource.Disconnect();

Next Step

Interact with Kaazing WebSocket Gateway Using the ByteSocket API

See Also

.NET and Silverlight Client API

TOP