# Node.js

{% hint style="success" %}
node.js에서는 `@hackler/javascript-sdk`를 이용해서 sdk를 연동할 수 있습니다.

`@hackler/hackle-sdk`를 사용하고 계시다면, `@hackler/javascript-sdk`로 마이그레이션 해야합니다.
{% endhint %}

## SDK 의존성 추가

[![](https://img.shields.io/npm/v/%40hackler%2Fjavascript-sdk)](https://www.npmjs.com/package/@hackler/javascript-sdk)

{% tabs %}
{% tab title="npm" %}

```shell
npm install --save @hackler/javascript-sdk
```

{% endtab %}

{% tab title="yarn" %}

```shell
yarn add @hackler/javascript-sdk
```

{% endtab %}
{% endtabs %}

## SDK 초기화

`hackleClient`는 SDK 기능을 사용하기 위한 메소드들을 제공하는 클래스입니다.

SDK 를 사용하기 위해서는 `hackleClient` 초기화가 필요합니다.

#### 인스턴스화

SDK 키를 전달하여 `hackleClient`를 인스턴스화 합니다.\
`hackleClient`는 필요한 정보들을 얻기 위해 백그라운드 작업으로 핵클 서버와 주기적으로 동기화합니다.

* SDK 키는 핵클 서비스의 대시보드 안에 위치한 [SDK 연동 정보](https://dashboard.hackle.io/config/sdk-setting)에서 확인하실 수 있습니다.

```javascript
const Hackle = require("@hackler/javascript-sdk");

// YOUR_SERVER_SDK_KEY 자리에 SDK 키를 넣습니다.
const hackleClient = Hackle.createInstance("YOUR_SERVER_SDK_KEY");
```

#### 초기화 완료

SDK 초기화 시 핵클 서버로부터 필요한 정보들을 가져옵니다.

초기화는 비동기로 진행되며 onReady로 초기화 완료 콜백을 받을 수 있습니다.

{% hint style="warning" %}
SDK가 준비되기 전 사용자 요청이 들어오면 **데이터가 누락될 수 있습니다.**
{% endhint %}

```javascript
hackleClient.onReady(() => {
  // SDK ready to use
  http.createServer((req, res) => {
  	
  }).listen(3000)
});
```

## 종료

어플리케이션이 종료될 때 `close()` 메소드를 호출해야 합니다. 이 과정을 통해 사용 중인 리소스를 반납하고 남아있는 이벤트를 전송합니다.

{% hint style="danger" %}
`close()` 호출 없이 어플리케이션이 종료되면 **이벤트가 누락될 수 있습니다.**
{% endhint %}

```javascript
process.on('SIGINT', function() {
  // kill signal
  hackleClient.close();
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hackle.io/development-guide/nodejs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
