> ## Documentation Index
> Fetch the complete documentation index at: https://developer.watson-orchestrate.ibm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# on()

Subscribes a handler function to a specific chat event. The handler will be called every time the event occurs.

## Syntax

```javascript theme={null}
instance.on(eventName, handler);
```

## Parameters

<ParamField path="eventName" type="string" required>
  The name of the event to subscribe to, for example, `'send'`, `'receive'`, `'chat:ready'`.
</ParamField>

<ParamField path="handler" type="function" required>
  The callback function to run when the event occurs. Receives `(event, instance)` as parameters.
</ParamField>

## Returns

<ResponseField name="void">
  Returns a `void` operator.
</ResponseField>

## Examples

The following examples show different ways to subscribe to events:

```javascript theme={null}
// Subscribe to send event
instance.on('send', (event, instance) => {
    console.log('Message sent:', event);
});

// Subscribe to receive event
instance.on('receive', (event, instance) => {
    console.log('Message received:', event);
});

// Subscribe to chat ready event
instance.on('chat:ready', (event, instance) => {
    console.log('Chat is ready');
    instance.send('Hello!');
});
```

## Considerations

### Common Events

* **`chat:ready`**\
  Fired when the chat is fully loaded and ready.

* **`pre:send`**\
  Fired before a message is sent.

* **`send`**\
  Fired after a message is sent.

* **`pre:receive`**\
  Fired before a message is received.

* **`receive`**\
  Fired after a message is received.

* **`feedback`**\
  Fired when user provides feedback.

* **`userDefinedResponse`**\
  Fired for custom response types.

* **`pre:restartConversation`**\
  Fired before conversation restart.

* **`restartConversation`**\
  Fired after conversation restart.

* **`pre:threadLoaded`**\
  Fired before a thread is loaded from history.

* **`authTokenNeeded`**\
  Fired when authentication token needs refresh.

* **`view:pre:change`**\
  Fired before view state changes.

* **`view:change`**\
  Fired after view state changes.

For more details about events, see [Event categories](events).

<Card title="Do you need practical examples?" icon="text-align-start" href="https://www.ibm.com/docs/SSAVQO/deploy/web_chat_agent/tutorials/tutorials_lp.html" arrow="true" cta="Learn with walk-throughs" horizontal>
  Learn how to apply the features available for embedded chat into your implementation with guidance and examples.
</Card>
