Consume API event-stream

Hi all,

I’m looking for the best way to consume the API event-stream to receive messages in “real time”. I know I can use curl to show it in the console but I want to know which language/framework I can use to develop my own backend/logic.
Anyone use the API event-stream for his own development?
Which language can be used to consume the API event-stream?

Real cases or examples will be useful.

Thanks you.

Hi,

any language is possible, best one which wraps sockets and HTTP protocol specs into a library. I’ve done sample implementations with Python and Golang already, and there are other consumers listed here.

I would pick one where you are familiar with, or has less dependencies to install/compile.

Cheers,
Michael

Thanks you @dnsmichi,

I’m not familiar with Python or GOlang, I was try with Node.js but no success… I will investigate.

Thanks you!

I know basic nodejs, maybe you can share your code attempt and we can look over it?

Hi @dnsmichi,

The problem is that I don’t know were to start. The API event stream use a POST method, so it’s a long polling request that I don’t know how to develop it in node.js.

I need to read more about that.
I will publish the solution if I find it, or maybe learn Python or Go hehehe

Thanks you!

Hi,

I finally solve it using Node.js, below I post the solution.
Packages needed:

  • https
  • agentkeepalive

Code:

const https = require('https');
const Agent = require('agentkeepalive').HttpsAgent;

const keepaliveAgent = new Agent({
  maxSockets: 100,
  maxFreeSockets: 10,
  freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
});

const options = {
  host: 'icinga_server',
  port: 5665,
  auth: 'username:password',
  path: '/v1/events?queue=cr&types=StateChange',
  method: 'POST',
  agent: keepaliveAgent,
  headers: {
        'Accept': 'application/json'
  }
};

makeRequest();

function makeRequest(){
  const req = https.request(options, res => {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      console.log('BODY: ' + chunk);
    });
  });
  req.on('error', e => {
    console.log('problem with request: ' + e.message);
    makeRequest();
  });
  req.end();

  }

setInterval(() => {
  if (keepaliveAgent.statusChanged) {
    if(keepaliveAgent.getCurrentStatus().resetStatus != 0){
            keepaliveAgent.setCurrentStatus();
            makeRequest();
    }
  }
}, 2000);

Custom modifications:
Each time the icinga2 restarts, the connections close the socket and it not reconnects. To handle that I modified the node_modules/agentkeepalive/lib/agent.js and added a new value called resetStatus and a new function setCurrentStatus, so each time the connection closes, the count reset to 0 and call the makeRequest function again.

1 Like

Awesome, thanks for sharing :muscle: :+1:

Hello all,

I create a GitHub repo with a full example code about this topic. You can check it and colaborate if you want: icinga2_api_request
Also you can chek an integration with for frontend in this one: icinga2_events_frontend

Best regards!