# Additional functions

## Run your own logic after message delivery

You can run your own arbitrary JS code before or after the onMessage gets executed.

```
var _M = _M || {}; //depending on when your script is executed, the _M namespace might exist or not.

_M.preEventHandler = function (incomingMessage) {
    console.log("Gets executed before the event handler gets called at all, so the first moment data comes into your website", incomingMessage);
};

_M.preRender = function (incomingMessage, pendingDomChange) {
    console.log("Gets executed before the render of an incoming DOM Change. The DOM change itself is built and ready to render.", incomingMessage);
};

_M.postRender = function (incomingMessage) {
    console.log("Gets executed after the render of an incoming DOM Change", incomingMessage);
};
```

## Send your own event from anywhere in the backend code

### To all users

```
ActiveSessionRegistry.getInstance().sendToAll( obj );
```

If you specifically want to send DOMChanges, you'll need to build it:

```
ActiveSessionRegistry.getInstance().sendToAll(DOMChanges.of("my-field", obj).build());
```

### To a specific user

```
ActiveSessionRegistry.getInstance().sendToSession(obj, sessionId);
```

TODO: A cache is used to determine if the last sent message has already been sent to the user. This allows you to call this method in intervals with duplicate events without having to worry about passing duplicate data to the user.


---

# 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://medusa-ui.gitbook.io/docs/medusa/additional-functions.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.
