Create and Publish a Server Sent Events API¶
This tutorial will guide you to create a SSE Streaming API which is connected to a backend that observes your system's memory, and gives you the values in a continuous manner. Follow the instructions in this tutorial to design and publish an API with an SSE backend, and thereafter invoke it using a cURL command.
Step 1 - Design a SSE API¶
-
Sign in to the Publisher.
https://<hostname>:9443/publisher
For testing purposes, you can use
https://localhost:9443/publisher
andadmin
as the username and password. -
Click CREATE API, go to Streaming API, and click SSE API.
Note
The CREATE button will only appear for a user who has the
creator
role permission. -
Enter the details of the new SSE API.
Field Sample value Name Observer Context /observer Version 1.0.0 Protocol SSE Endpoint http://localhost:8080 -
Click CREATE.
The overview page of the created SSE API appears.
-
Attach business plans to the SSE API.
Now, you have created and configured the SSE API successfully.
Step 2 - Publish the SSE API¶
-
Click Lifecycle to navigate to the API lifecycle page.
-
Click Publish to publish the API to the API Developer Portal.
-
Click Deployments to navigate to the Deployments page and click Deploy New Revision.
-
Select Production and Sandbox, choose localhost as the VHost, and click on Deploy.
Step 3 - Start the SSE Server¶
-
Download the sample SSE server from WSO2 APIM Samples - GitHub repository.
-
Go to the directory where you downloaded the SSE server and run the following command.
java -jar sse-server-1.0.0.jar --time=5000 --interval=1000
Step 4 - Invoke the SSE API¶
-
Sign in to the Developer Portal.
https://<hostname>:9443/devportal
For testing purposes, you can use
https://localhost:9443/devportal
andadmin
as the username and password. -
Click on the SSE API.
The API overview appears.
-
Subscribe to the API.
-
Click Subscriptions to go to the Subscriptions page and click SUBSCRIPTION & KEY GENERATION WIZARD.
This wizard takes you through the steps of creating a new application, subscribing, generating keys, and generating an access token to invoke the API.
Note
You can use any application (e.g., JWT or OAuth) to subscribe to the API.
-
Copy the authorization token that appears, and click FINISH.
-
-
Try out the operations.
Invoke the API with an authorization header by executing the following cURL command.
curl http://localhost:8280/observer/1.0.0/memory -H "Authorization: Bearer [accesstoken]"
You will receive continuous stream of events showing your memory usage, from the server.
data:{"heap":67893392,"nonHeap":36260800,"ts":1614803952066,"identifier":"ram_222"} data:{"heap":72591160,"nonHeap":37250808,"ts":1614803953067,"identifier":"ram_223"} data:{"heap":72591160,"nonHeap":37251544,"ts":1614803954064,"identifier":"ram_224"}
You have successfully created and published your first SSE API, subscribed to it, obtained an access token for testing, and tested your API with an access token.
Top