# Consume API event-stream

**URL:** <https://community.icinga.com/t/consume-api-event-stream/1010>\
**Category:** Icinga 2\
**Tags:** nodejs, api\
**Created:** [April 24, 2019, 1:23pm UTC](https://community.icinga.com/t/consume-api-event-stream/1010 "2019-04-24T13:23:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![proquibas](https://community.icinga.com/user_avatar/community.icinga.com/proquibas/32/237_2.png) [@proquibas](https://community.icinga.com/u/proquibas)\
**Post date:** [April 24, 2019, 1:23pm UTC](https://community.icinga.com/t/consume-api-event-stream/1010/1 "2019-04-24T13:23:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dnsmichi](https://community.icinga.com/user_avatar/community.icinga.com/dnsmichi/32/7721_2.png) [@dnsmichi](https://community.icinga.com/u/dnsmichi)\
**Post date:** [April 24, 2019, 2:43pm UTC](https://community.icinga.com/t/consume-api-event-stream/1010/2 "2019-04-24T14:43:37Z")

</div>

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](https://github.com/Icinga/icinga2-api-examples#event-streams).

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

Cheers,  
Michael

---

<div class="post-metadata">

**Author:** ![proquibas](https://community.icinga.com/user_avatar/community.icinga.com/proquibas/32/237_2.png) [@proquibas](https://community.icinga.com/u/proquibas)\
**Post date:** [April 25, 2019, 7:56am UTC](https://community.icinga.com/t/consume-api-event-stream/1010/3 "2019-04-25T07:56:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dnsmichi](https://community.icinga.com/user_avatar/community.icinga.com/dnsmichi/32/7721_2.png) [@dnsmichi](https://community.icinga.com/u/dnsmichi)\
**Post date:** [April 25, 2019, 8:02am UTC](https://community.icinga.com/t/consume-api-event-stream/1010/4 "2019-04-25T08:02:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![proquibas](https://community.icinga.com/user_avatar/community.icinga.com/proquibas/32/237_2.png) [@proquibas](https://community.icinga.com/u/proquibas)\
**Post date:** [April 25, 2019, 8:54am UTC](https://community.icinga.com/t/consume-api-event-stream/1010/5 "2019-04-25T08:54:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![proquibas](https://community.icinga.com/user_avatar/community.icinga.com/proquibas/32/237_2.png) [@proquibas](https://community.icinga.com/u/proquibas)\
**Post date:** [April 26, 2019, 7:24am UTC](https://community.icinga.com/t/consume-api-event-stream/1010/6 "2019-04-26T07:24:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dnsmichi](https://community.icinga.com/user_avatar/community.icinga.com/dnsmichi/32/7721_2.png) [@dnsmichi](https://community.icinga.com/u/dnsmichi)\
**Post date:** [April 26, 2019, 7:52am UTC](https://community.icinga.com/t/consume-api-event-stream/1010/7 "2019-04-26T07:52:01Z")

</div>

Awesome, thanks for sharing 💪 👍

---

<div class="post-metadata">

**Author:** ![proquibas](https://community.icinga.com/user_avatar/community.icinga.com/proquibas/32/237_2.png) [@proquibas](https://community.icinga.com/u/proquibas)\
**Post date:** [May 4, 2020, 4:02pm UTC](https://community.icinga.com/t/consume-api-event-stream/1010/8 "2020-05-04T16:02:54Z")

</div>

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](https://github.com/jtejedera/icinga2_api_request)  
Also you can chek an integration with for frontend in this one: [icinga2\_events\_frontend](https://github.com/jtejedera/icinga2_events_frontend)

Best regards!
