How to use Long Polling to Receive Events

Learn how to receive events using the long polling method in the client.

Long polling

The example code below explains how to receive events in real-time by utilizing the API key of EQ Hub and the API implemented in the long polling method.

For a detailed explanation of the long polling method, refer to here.

The library information used in the example code is as follows.

  • axios: ^1.4.0
import axios, { AxiosInstance } from "axios";

const API_KEY: string = 'YOUR_API_KEY';
const END_POINT: string = 'https://ag.eqhub.eqbr.com'
let server: AxiosInstance;

server = axios.create({
  baseURL: END_POINT,
})

server.defaults.headers.common['x-eq-ag-api-key'] = API_KEY;

async function connection() {
  let response;
  try{
      console.log('enter connection')
      response = await server.get("Long Polling API URL");
      return response.data
    }catch (e) {
      if(e.status === 502){
        // HTTP Status 502 means timeout status, and occurs when the server waits for a response
        // beyond the set time.
				// The connection to the server was lost, so reconnect..
        return await connection();
      }else if(e.status === SHOULD_RECONNECT_ERROR){
        // When an error occurs, if the error requires reconnection to the server, reconnect.
        return await connection();
      }
    }
  }

Concluding

Through the above process, we learned how the client receives events from the server using the REST API implemented in the Long Polling method.