Server Side Events (SSE)
📘 Note
This feature is only available for web apps that integrate with Console.
The Server Side Events (SSE) feature enables app developers to build more responsive web applications with the ability to receive real-time response data directly from the Console UI. This is achieved through a Server-Sent Event communication, enabling a seamless and immediate data flow from the Console to the web app upon initiating an API request. The native push-based data delivery not only enables faster, real-time frontend experiences but also streamlines infrastructure efficiency.
📘 Note
The SSE client ID has a lifespan of 1 hour. If this limit is exceeded, the API will throw an error. To establish a connection again, you need to refresh the page.
To Implement SSE, you need to instantiate the OS1HttpClient from the Console UI. The OS1HttpClient is a wrapper HTTP client provided by the Console UI to make it easier to support POST, PUT, PATCH, DELETE HTTP methods necessary for RESTful API calls. It is a pre-configured instance of Axios that handles authentication, base URLs, and other HTTP client best practices.
The OS1HttpClient takes two arguments:
- The first is props.console.authInitializer, which contains the authentication context or initialization parameters necessary for the client to make authenticated requests.
- The second argument is the process.env.BASE_URL is for the API endpoints that the application will communicate with.
This guide details the integration of Server-Side Events (SSE) using the OS1HttpClient class provided by the Console, allowing for real-time communication from server to client in your application. There are two ways to implement Server Side Events (SSE) in your application:
This is the simplest option if using the Console’s Axios client.
When making an API call, specify the attribute where the callback url is expected on the backend and its value needs to be set by using the placeholder {{SSE_CALLBACK}}. The Console will automatically detect and replace this placeholder with the actual callback URL. Developers can pass the callback attribute in the request URL, headers, or the request body.
The example below adds {{SSE_CALLBACK}} to the header for the createVehicles request.
Internally, the OS1HttpClient will manage the SSE connection and retrieve the callback URL from the server if the {{SSE_CALLBACK}} placeholder is present and will replace the variable where the callback URL is needed in the API request.
Once a callback is received from the server, the Console will emit a custom event, SSECallBackEvent. Your application needs to listen to this event in your UI code and handle the data when the API response is pushed from the server.
The OS1HttpClient can handle DELETE requests with SSE. Users can pass {{SSE_CALLBACK}} in the headers, data payload, or query parameters, allowing the console to intercept the request and replace {{SSE_CALLBACK}} with the actual SSE callback URL.
The example below shows a DELETE request by passing {{SSE_CALLBACK}} in both the header and payload.
If there are any internal errors during connection setup, the Console will throw an error, and an API call will be made with null as the callback URL. Only non-retriable errors will be thrown to the user.
The following sample code demonstrates the full implementation for using the {{SSE_CALLBACK}} placeholder:
The implementation does the following:
- Imports and instantiates the OS1HttpClient
- Makes an API call with the {{SSE_CALLBACK}} placeholder
- Console will swap the placeholder with the actual URL
- Listens for the SSECallbackEvent callback
- The event data contains the response payload.
This option works with any HTTP client and is not limited to Console’s Axios client.
When you are ready to make an API call, ensure that the getEventBrokerUrl function is invoked to verify or refresh the callback URL. This function will maintain the connection and provide the callback URL for the API request. See the example code below:
Store the returned callback URL in a component state variable or global context object. This allows referencing it when making API calls.
Make your API request by passing the callback URL as a parameter, header, or elsewhere in the request.
Once a callback is received from the server, the Console will emit a custom event, SSECallBackEvent. Your application can listen to this event in your UI code and handle the data when the API response is pushed from the server.
Developers can use the useEffect hook to optimize the state management and avoid unnecessary re-fetching. See the following code below:
The following example shows how to implement SSE into an application by manually calling the getEventBrokerUrl:
The implementation does the following:
- Imports and instantiates HTTP client
- Manages callback URL state
- Calls useEffect to fetch initial URL
- Calls the getEventBrokerUrl before each request
- Only updates the state if the URL changed
- Passes the callbackUrl state in the request
- Listens for SSECallbackEvent