# AFK Module

### Overview

The AFK (Away-From-Keyboard) functionality detects when a user has been inactive for a configured amount of time. When inactivity is detected, the Pixel Streaming system can display a warning and eventually terminate the session - which is mainly relevant as cost saving functionality.

In the Arcware WebSDK, AFK events are emitted by the **Epic Games Pixel Streaming Infrastructure** and forwarded through the `PixelStreaming` instance. The Arcware SDK does not implement its own AFK logic but exposes the events so developers can react to them.

These events are available for both:

| Mode                             | Supported |
| -------------------------------- | --------- |
| ArcwareInit (UI integration)     | ✔         |
| CoreSetup (Headless integration) | ✔         |

AFK behaviour such as timeout duration is primarily configured in the **Arcware Cloud platform settings** or project configuration.

***

## Accessing AFK Events

AFK events can be listened to through the `PixelStreaming` instance using the standard Pixel Streaming event system.

Example:

```typescript
PixelStreaming.addEventListener("eventName", callbackFunction);
```

Example setup:

```typescript
const { PixelStreaming } = ArcwareInit(
  { shareId: "<your-share-id>" },
  configuration
);
```

***

## Available AFK Events

| Event Name             | Epic Pixel Streaming Event  |
| ---------------------- | --------------------------- |
| `afkWarningActivate`   | `AfkWarningActivateEvent`   |
| `afkWarningDeactivate` | `AfkWarningDeactivateEvent` |
| `afkWarningUpdate`     | `AfkWarningUpdateEvent`     |
| `afkTimedOut`          | `AfkTimedOutEvent`          |

These events represent different stages of inactivity detection.

***

## Event Lifecycle

The typical AFK lifecycle follows this sequence:

```
User inactive
      ↓
afkWarningActivate
      ↓
afkWarningUpdate (countdown updates)
      ↓
afkTimedOut
```

If the user interacts again before the timeout occurs:

```
User inactive
      ↓
afkWarningActivate
      ↓
User interaction detected
      ↓
afkWarningDeactivate
```

***

## Event Descriptions

### afkWarningActivate

Triggered when the system detects inactivity and activates the AFK warning state.

Typical use cases:

* show a custom AFK overlay
* notify the user they are about to be disconnected

Example:

```typescript
PixelStreaming.addEventListener("afkWarningActivate", () => {
  console.log("AFK warning activated");
});
```

***

### afkWarningDeactivate

Triggered when the AFK warning is cleared because user activity was detected.

Example:

```typescript
PixelStreaming.addEventListener("afkWarningDeactivate", () => {
  console.log("User became active again");
});
```

Typical uses:

* hide custom AFK overlays
* reset inactivity timers

***

### afkWarningUpdate

Triggered periodically while the AFK warning is active. This event is typically used to update a **countdown display**.

Example:

```typescript
PixelStreaming.addEventListener("afkWarningUpdate", (event) => {
  console.log("AFK countdown update:", event);
});
```

Typical payload (example):

```typescript
{
  remainingTime: number
}
```

Example usage:

```typescript
PixelStreaming.addEventListener("afkWarningUpdate", (event) => {
  document.getElementById("afk-countdown").innerText =
    `${event.remainingTime}`;
});
```

***

### afkTimedOut

Triggered when the AFK timeout has been reached and the session will be terminated.

Example:

```typescript
PixelStreaming.addEventListener("afkTimedOut", () => {
  console.log("Session timed out due to inactivity");
});
```

Typical uses:

* show a timeout message
* redirect the user
* offer a reconnect button

Example:

```typescript
PixelStreaming.addEventListener("afkTimedOut", () => {
  document.getElementById("timeout-overlay").style.display = "block";
});
```

***

## AFK Configuration

AFK behavior is usually configured in the **Arcware Cloud platform** rather than directly in the WebSDK.

Although Pixel Streaming exposes parameters such as:

```
TimeoutIfIdle
AFKTimeout
AFKCountdown
```

these values may be overridden by backend configuration depending on the project or share settings.

For this reason, the recommended way to configure AFK behavior is through:

* project settings
* share configuration
* Arcware Cloud platform controls

***

## Default AFK Overlays

When using the **Arcware UI integration (`ArcwareInit`)**, the Pixel Streaming infrastructure may automatically show default AFK warning overlays.

These overlays typically include:

* inactivity warning messages
* countdown timer
* session termination notice

If you want to implement **fully custom UI**, you can ignore the default overlays and build your own using the AFK events described above.

The SDK also provides a configuration option that can hide the default AFK overlay:

```typescript
settings: {
  whiteLabelling: {
    hideAfkOverlay: true
  }
}
```

***

## Example: Custom AFK Overlay

Example implementation of a custom AFK overlay:

```typescript
PixelStreaming.addEventListener("afkWarningActivate", () => {
  document.getElementById("afk-overlay").style.display = "block";
});

PixelStreaming.addEventListener("afkWarningDeactivate", () => {
  document.getElementById("afk-overlay").style.display = "none";
});

PixelStreaming.addEventListener("afkTimedOut", () => {
  document.getElementById("afk-overlay").innerText =
    "Session ended due to inactivity.";
});
```

Example UI:

```html
<div id="afk-overlay" style="display:none">
  <p>You have been inactive.</p>
  <p>Interaction is required to keep the session active.</p>
</div>
```

***

## Notes

* AFK events originate from the **Epic Pixel Streaming Infrastructure**.
* The Arcware WebSDK exposes these events through the `PixelStreaming` instance.
* AFK timeout configuration is usually managed through the **Arcware Cloud platform**, not through the WebSDK itself.


---

# 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.arcware.cloud/web-integration/new-websdk/in-depth/afk-module.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.
