on() & off()

Subscribe to SDK lifecycle, placement, and user interaction events with on(), and remove handlers with off() or the unsubscribe function returned by on().

The event subscription API lets you react to what happens inside the Inline AI SDK: when the SDK initializes, when placements mount or unmount, and when users open or close the search overlay or widget. on() registers a handler for any named event and returns a function that unsubscribes it. off() provides an alternative way to unsubscribe using the original handler reference.

Both methods work before init() is called, making them safe to use in hybrid mode or in the command queue before the SDK has fully loaded.

You can subscribe to events via the command queue using the array syntax: window.InlineAI.cmd.push(['on', 'search:open', handler]). This is particularly useful in hybrid mode where you want to attach listeners before the SDK initializes.

on(event, handler)

on(event: SDKEventName, handler: (...args: unknown[]) => void): () => void

Subscribes to a named SDK event. When the event fires, handler is called with event-specific payload data. The method returns an unsubscribe function; call it to stop listening without needing to keep a reference to the original handler.

Parameters

eventSDKEventNameRequired

The event name to subscribe to. You can use raw string literals or constants from the Events enum; both are equivalent. See the events reference for the full catalog, payload shapes, and per-event examples.

handlerfunctionRequired

The callback function to invoke when the event fires. The argument shape depends on the event; see the table above for each event's payload.

Return value

unsubscribe() => void

A function that, when called, removes this specific subscription. Equivalent to calling off(event, handler) with the same handler reference.


off(event, handler)

off(event: SDKEventName, handler: (...args: unknown[]) => void): void

Unsubscribes a previously registered handler. You must pass the same function reference that was passed to on(). Anonymous functions cannot be unsubscribed with off(), which is why on() also returns an unsubscribe function as a convenient alternative.

Parameters

eventSDKEventNameRequired

The event name the handler was subscribed to.

handlerfunctionRequired

The exact function reference that was passed to on(). If the reference does not match any registered handler, the call is silently ignored.


Examples

var unsubscribe = window.InlineAI.on('search:open', function(data) {
  console.log(data.query);
});
 
// Stop listening later
unsubscribe();

The ready event fires once, immediately after init() completes. If you subscribe after the SDK is already initialized, the handler will not fire retroactively. Subscribe early, or check getPlacements() to inspect current state.

© 2026 Inline AI