WSO2 Complex Event Processor is succeeded by WSO2 Stream Processor. To view the latest documentation for WSO2 SP, see WSO2 Stream Processor Documentation.
||
Skip to end of metadata
Go to start of metadata

Introduction

This sample demonstrates how the WSO2 CEP can publish events to a Websocket client. 

Refer to below image to get an overview of the event flow.

Refer to the below steps, which we are going to do in this sample:

  • Step 1: Start CEP server. This will start a Websocket server which is in-built to CEP.
  • Step 2: Define an Event Stream in CEP.
  • Step 3: Create an Output Websocket-local Event Adapter, so events of type defined in step 1 will be published via the CEP Websocket server.
  • Step 4: Connect a Websocket client to the Websocket Server in CEP. 
  • Step 5: Send a few events to the CEP. To do this, we will use the Event Simulator, in-built to CEP. As the events are sent, Websocket client will receive the same.

Prerequisites

See Prerequisites in CEP Samples Setup page. 

In addition to that, go to <CEP_HOME>/samples/cep/utils/output-websocket-local-adapter folder and execute the following command:

ant -Dsn=0070

This will copy outputwebsocket.war webapp to <CEP_HOME>/sample/artifacts/0070/webapps folder. 

Starting sample CEP configurations 

Start the WSO2 CEP server with the sample configuration numbered 0070 For instructions, see Starting sample CEP configurations.

This sample configuration points the default Axis2 repo to <CEP_HOME>/samples/cep/artifacts/0070 (by default, the Axis2 repo is <CEP_HOME>/repository/deployment/server). As a result, the artifacts in <CEP_HOME>/sample/artifacts/0070 folder will get deployed.

When these sample configurations are deployed:

  • An event stream will be defined.
  • The event simulator will be ready to play a set of events to CEP. 
  • An Output Websocket-local Event Adapter will be created.

In other words, all the first three steps listed in Introduction section will be done, when the server is started with sample configuration number 0070

Now the remaining tasks are to execute steps 4 and 5: connect a Websocket client, and send a few events to the CEP.

Connect to CEP via a Websocket client (to receive events)

In this sample, you will use your web browser as the Websocket client. However, any Websocket client can be used for this purpose.

Step I: Start a web browser and go to its Javascript console. In most browsers, such as Chrome and Firefox, you can load the Javascript console by simply pressing the keys Ctrl+Shift+J (or Cmd+Shift+J on a Mac). 

Ensure that the page on which you open the Javascript Console is loaded over HTTP. For example, you can open the page http://wso2.com and load the Javascript console on that page.

Step II: On the browser console, copy and paste the following Javascript code block and press enter:

var ws = new WebSocket("ws://localhost:9763/outputwebsocket/wsLocalOutputAdapter");
ws.onopen = function() {
console.log("opened");
};
ws.onmessage = function (evt) {
alert("Message: " + evt.data); 
};
ws.onclose = function() {
console.log("closed!");
};
ws.onerror = function(err) {
console.log("Error: " + err);
};

Note: If you have started CEP server on a different host and a port, replace 'localhost' in the above command with that host, and '9763' with that port, respectively. See Running the Product page for more details.

This step is shown in the screenshot below.


Upon successful connection, you will see an output in the same console as shown in the below screenshot.

Now this browser-client is ready to receive events from the CEP.

Step III: Next and final step is to send four events to the CEP. We will send the following four events of type org.wso2.event.sensor.stream to the CEP.

199008131245,false,100,temperature,23.45656,7.12324,100.34,23.4545
199008131245,true,101,temperature,23.45656,7.12324,100.34,23.4545
199008131245,false,103,temperature,23.45656,7.12324,100.34,23.4545
199008131245,true,104,temperature,23.45656,7.12324,100.34,23.4545

For sending events, we will be using the CEP event simulator.

To use the event simulator, log in to CEP management console, go to Tools --> Event Simulator.

In the Event Simulator page, under 'Send multiple events', you will see an input data file being ready to be played. Once we click 'play', the events included in this data file will be sent to the CEP.

Click 'play' button, as shown in the screenshot below.


As the events are being played, you will see four alerts from your browser, indicating the receipt of events. One such alert is shown in the image below.


  • No labels