# Notifications

> How people learn about their tasks. A server-sent event stream pushes new tasks to their assignee, and the React SDK turns task events into toasts with actions, including due-soon and overdue reminders.

Source: https://docs.sommatic.ai/en/human-in-the-loop/notifications/ (updated 2026-11-26)

A task only works if the right person sees it in time. Sommatic AI notifies at two levels: the backend pushes new tasks to
the person they were assigned to, and the browser shows them as toasts that open, claim or snooze the task.

## The task stream

`GET /work-management/task/stream` is a server-sent events (SSE) stream for one person. Pass the person's identity as
`user[identity]`:

```text
GET /work-management/task/stream?user[identity]=<user-identity>
```

| Event           | When                                                     | Data                                               |
| --------------- | -------------------------------------------------------- | -------------------------------------------------- |
| `connected`     | Right after the stream opens.                            | `{ "message": "Connected", "user_identity": "…" }` |
| `task-assigned` | A task is created with this person as its user assignee. | `{ "kind": "assigned", "task": { … } }`            |
| `error`         | The identity is missing or the stream failed.            | `{ "message": "…" }`                               |

The server sends a keep-alive comment every 30 seconds. A person can hold several streams at once (one per tab); each
one receives the events.

> **Note: Only new tasks are pushed**
>
> `task-assigned` fires when a task is created with a user assignee, for example by `human.task.create` with an
> assignee. Transitions (claim, complete, reject…) are not pushed on this stream.

### Listen with the React SDK

`WorkManagementTaskStreamService` from `@sommatic/react-sdk` wraps the stream. It reconnects on its own after an error.

```javascript
import { WorkManagementTaskStreamService } from '@sommatic/react-sdk';

const taskStream = new WorkManagementTaskStreamService();
taskStream.setParams({ user: { identity: currentUser.identity } });

taskStream.on('task-assigned', ({ task }) => {
  showNewTask(task);
});

taskStream.connect();

// When the view unmounts
taskStream.disconnect();
```

## Toasts in the browser

In the browser, task notifications are DOM events with the `sommatic::` namespace. Any module can emit them, and the
`TaskNotificationToast` component of `@sommatic/react-sdk` shows them.

```jsx
import { TaskNotificationToast } from '@sommatic/react-sdk';

export default function Layout({ children }) {
  return (
    <>
      {children}
      <TaskNotificationToast />
    </>
  );
}
```

### `sommatic::task-notification`

Emit it to show a toast:

```javascript
window.dispatchEvent(
  new CustomEvent('sommatic::task-notification', {
    detail: {
      kind: 'assigned',
      task: {
        id: '<task-id>',
        title: 'Approve billing exception',
        priority: { id: 4, name: 'critical', title: 'Critical', color: '#d32f2f' },
        due_at: '1761916800000',
      },
      actions: [
        { id: 'open', title: 'Open' },
        { id: 'claim', title: 'Claim' },
      ],
    },
  }),
);
```

| Field     | Required | What it is                                                              |
| --------- | -------- | ----------------------------------------------------------------------- |
| `kind`    | Yes      | `assigned`, `dueSoon`, `overdue` or `completed`.                        |
| `task`    | Yes      | `id` and `title`; optionally `type`, `priority`, `status` and `due_at`. |
| `message` | No       | `title` and `subtitle` that replace the defaults.                       |
| `actions` | No       | Up to two buttons: `{ id, title, payload? }`.                           |
| `meta`    | No       | `source` and `timestamp`.                                               |

| `kind`      | Severity                                       | Closes after       |
| ----------- | ---------------------------------------------- | ------------------ |
| `assigned`  | info (warning when the priority is `critical`) | 6 s                |
| `dueSoon`   | warning                                        | 10 s               |
| `overdue`   | error                                          | Stays until closed |
| `completed` | success                                        | 4 s                |

The toast respects the person's notification preferences (pause, quiet hours, priorities, muted types) and shows at
most three toasts in any 8 seconds, so a burst of events never buries the screen.

### `sommatic::task-notification-action`

When a person clicks a toast button, the toast emits this event with `task_id`, `action_id` (`open`, `claim`,
`snooze`, `view`) and an optional `action_payload`. Your application decides what to do:

```javascript
window.addEventListener('sommatic::task-notification-action', (event) => {
  const { task_id, action_id } = event.detail;

  if (action_id === 'open') {
    openTask(task_id);
  }

  if (action_id === 'claim') {
    claimTask(task_id);
  }
});
```

`snooze` is handled by the toast: it silences `dueSoon` toasts for that task for 10 minutes by default (set
`action_payload.minutes` to change it). The snooze is kept in the browser's `localStorage`.

## Due-soon and overdue reminders

`useTaskSLAMonitor` polls the active tasks of the person (every 15 minutes by default) and emits `dueSoon` when a task
is due in less than 30 minutes and `overdue` when its `due_at` has passed. Each task is notified once per kind. The
first check after the page loads is silent, so tasks that were already late do not all pop up at once.

```javascript
import { useTaskSLAMonitor } from '@sommatic/react-sdk';

useTaskSLAMonitor(fetchActiveTasks, {
  enabled: Boolean(user?.identity),
  intervalMs: 60000,
  dueSoonThresholdMs: 1800000,
});
```

`fetchActiveTasks` is an async function that returns the person's open tasks, for example from
`GET /work-management/task/inbox`.

## Next steps

- [Task lifecycle](https://docs.sommatic.ai/en/human-in-the-loop/task-lifecycle.md): The transitions behind each notification.
- [React SDK hooks](https://docs.sommatic.ai/en/sdks/react/hooks.md): Task inbox, streams and more.
