> For the complete documentation index, see [llms.txt](https://docs.happenn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.happenn.com/for-dev/happenn-io.md).

# Happenn IO

Explain the structure and how the socket interact with other applications.

## Introduction

This application handle real-time transfer data via web socket. This make something like Chat, Vote, and Poll can show and change the data in real-time instead of wait until the user refresh the page to update the data. In most case, only Happenn API will be the only application that request the emit data (tell the server to send specific data to the page that has an on(\<event>) with the same event name and let the code handle the data). There are also some case which we let the client handle the socket directly, such as join room, leave room, and get the connection in the room.

## Node.js, Socket.IO, and Express

**Node.js** - as we're using **Socket.IO**. It requires Node.js to function.

**Socket.IO** - A simple web socket library. Make the client and server transfer the data in real-time via ws/wss protocol.

**Express** - Node.js framework. We're using it for routing in general.

## Socket Code

*locate: src/socket.js*

There are 3 main events that has specific code

1. **join-room** - Use for when the user join something either access the page or enter the session room. This help for logging and get the number of connect in the room.
2. **leave-room** - Use for when the user close the connection either from closing the browser/tab or internet connection not working. This also sending logging.
3. **emit-room-count** - Use for staying in sync with the number of connection inside the room to be able to update the connection amount in real-time.

You can check the send log code at the top of the file via function **sendLog**.

## Force Emit Event via API

*locate: src/Routes/api/socket.js*

## Emit Data

<mark style="color:green;">`POST`</mark> `https://io.happenn.com/socket`

#### Request Body

| Name  | Type   | Description                    |
| ----- | ------ | ------------------------------ |
| key   | string | Secret key                     |
| event | string | Event name to emit the data to |
| data  | string | JSON encode string.            |

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

```
```

{% endtab %}
{% endtabs %}
