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.
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:
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.
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.
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.