---
title: "Pull (Push & Pull)"
description: "Real-time message stream from Bitrix24 to your application — WebSocket primary, long-polling fallback. Frame-only."
canonical_url: "https://b24-js-sdk.bx-shef.by/docs/entry-points/pull"
last_updated: "2026-12-01"
---
# Pull (Push & Pull)

> Real-time message stream from Bitrix24 to your application — WebSocket primary, long-polling fallback. Frame-only.

> [!CAUTION]
> The Pull client is meant for **frame applications** (`B24Frame`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""}) — it relies on the parent-window auth, channels, and `member_id`. It is **not** functional through `B24Hook`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} or `B24OAuth`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} on the server.

## Overview

Pull lets the front-end of your application receive events the back-end (or another front-end instance) emits via the [`pull.application.event.add`](https://apidocs.bitrix24.com/api-reference/interactivity/push-and-pull/pull-application-event-add.html){rel="[\"nofollow\"]"} REST method. The SDK ships a complete client (`B24PullClientManager`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""}) with two connectors:

- WebSocket primary (`ConnectionType.WebSocket`)
- Long-polling fallback (`ConnectionType.LongPolling`)
- Protobuf-encoded message envelopes (decoders bundled, no extra dependency)

You normally drive Pull through [`useB24Helper`](https://b24-js-sdk.bx-shef.by/raw/docs/entry-points/helper-use-b24-helper.md) — `usePullClient` / `useSubscribePullClient` / `startPullClient` are thin wrappers over the matching [`B24HelperManager`](https://b24-js-sdk.bx-shef.by/raw/docs/entry-points/helper.md) methods.

## Quick Start

```ts
import {
  initializeB24Frame,
  useB24Helper,
  Text,
  LoggerFactory,
  type B24Frame,
  type TypePullMessage
} from '@bxshefby/b24jssdk'

const $logger = LoggerFactory.createForBrowser('MyApp', import.meta.env?.DEV === true)
const {
  initB24Helper,
  getB24Helper,
  usePullClient,
  useSubscribePullClient,
  startPullClient,
  destroyB24Helper
} = useB24Helper()

let $b24: B24Frame

async function init() {
  $b24 = await initializeB24Frame()
  await initB24Helper($b24)

  // 1. Поднимаем внутренний B24PullClientManager (по одному на экземпляр хелпера)
  usePullClient()

  // 2. Подписка — можно вызывать несколько раз для разных moduleId
  useSubscribePullClient((message: TypePullMessage) => {
    $logger.info(`${Text.getDateForLog()} << pull`, { message })
  }, 'main')

  // 3. Подключение (WebSocket → запасной long-polling)
  startPullClient()
}

async function emitPing(): Promise<void> {
  await $b24.actions.v2.call.make({
    method: 'pull.application.event.add',
    params: {
      COMMAND: 'ping',
      PARAMS: { tick: Text.getDateForLog() },
      MODULE_ID: getB24Helper().getModuleIdPullClient()
    }
  })
}

await init()
setInterval(emitPing, 5000)
```

When the host component unmounts, call `destroyB24Helper()` — that fully tears the Pull client down: closes the WebSocket, clears all subscriptions, removes its window listeners, and cancels all pending timers.

## API via `B24HelperManager`

These four methods are the public Pull surface. See [B24HelperManager](https://b24-js-sdk.bx-shef.by/raw/docs/entry-points/helper.md#%D1%81%D0%B2%D1%8F%D0%B7%D0%BA%D0%B0-%D1%81-%D0%BA%D0%BB%D0%B8%D0%B5%D0%BD%D1%82%D0%BE%D0%BC-pull).

```ts-type
usePullClient(prefix?: string, userId?: number): B24HelperManager
subscribePullClient(callback: (message: TypePullMessage) => void, moduleId?: string): B24HelperManager
startPullClient(): void
getModuleIdPullClient(): string
```

- `usePullClient` constructs `B24PullClientManager`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} once. `prefix` is forwarded to `b24.auth.getUniq(prefix)` to derive `restApplication`. `userId` defaults to the loaded profile id.
- `subscribePullClient` returns the helper for chaining; the unsubscribe handle is tracked internally and released on `destroy()`.
- `startPullClient` triggers `PullClient.start()` and logs the failure (without throwing) if the connection cannot be established (see [Reconnection](#reconnection)).
- `getModuleIdPullClient` returns the `moduleId` last passed to `subscribePullClient`. Use it as the `MODULE_ID` parameter of `pull.application.event.add`.

## API via `B24PullClientManager` Directly

If you need finer control, instantiate the client yourself:

```ts
import { B24PullClientManager } from '@bxshefby/b24jssdk'

const pull = new B24PullClientManager({
  b24: $b24,
  restApplication: $b24.auth.getUniq('myApp'),
  userId: 1
})

const unsubscribe = pull.subscribe({
  type: 'server', // 'server' | 'client' | 'online'
  moduleId: 'main',
  command: 'optionsChanged',
  callback: (params) => console.log(params)
})

await pull.start()

// Позже
unsubscribe()
pull.destroy()
```

`subscribe()` accepts either a full `TypeSubscriptionOptions`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} object (filter by `type` / `moduleId` / `command`) or a bare `TypeSubscriptionCommandHandler`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} function (catches any incoming command). It returns an unsubscribe callback.

## Connection Types

The client picks the connector automatically:

1. WebSocket — preferred when the portal exposes one.
2. Long-polling — used when WebSocket is unavailable or blocked. A dropped WebSocket is reconnected as a WebSocket; the client does not switch to long-polling on its own.

`ConnectionType`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} (`Undefined / WebSocket / LongPolling`) and `PullStatus`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} (`Online / Offline / Connecting`) are exported from the package — useful if you want to reflect the connection in your UI.

## Reconnection

The client behaves like the default Push & Pull client of the Bitrix24 core (the one portals load for every user):

- A dropped connection is retried after 0.5 s, then 15 s twice, 45 s twice, then every 10 minutes up to the tenth attempt and every hour after that (each with up to 20 % jitter). The browser's `online` event triggers an immediate attempt.
- If the push-server config cannot be loaded, `start()` rejects its promise and does not retry — call `start()` again or reload the page.
- Once the client has started, it checks the config every 60 s and restarts when the config has expired; a failed restart is retried by the next check. If a restart gets 401 or 403 (or the portal answers `AUTHORIZE_ERROR` / `WRONG_AUTH_TYPE`), the check stops and `onPullError` receives `AUTHORIZE_ERROR`.
- The config — including the push-server token and the channel signatures — is cached in `localStorage`, as the core client does, so a page load does not ask the portal again. It is readable by any script of the same origin. A config you pass to `start(config)` is not cached; `restart()` and `destroy()` remove the cache.
- `stop()` and `destroy()` also cancel a restart that is already in flight.

## Sending Messages

The Pull client only **receives** messages. To send, call `pull.application.event.add` over REST:

```ts
await $b24.actions.v2.call.make({
  method: 'pull.application.event.add',
  params: {
    COMMAND: 'optionsChanged',
    PARAMS: { theme: 'dark' },
    MODULE_ID: getB24Helper().getModuleIdPullClient()
  }
})
```

Subscribers attached via `useSubscribePullClient(cb, 'main')` will receive the matching `TypePullMessage`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""}.

> [!CAUTION]
> `PullClient` also exposes `sendMessage()` and `sendMessageToChannels()`.
> **`sendMessage()` is not the supported path in an application**: it resolves
> the recipients' channels through `pull.channel.public.list`, which is not part
> of the application REST surface, and rejects with
> `JSSDK_PULL_PUBLIC_IDS_UNAVAILABLE`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} when it has to make that
> call. Use `pull.application.event.add`, as above.
> It does not always have to. The lookup is skipped when every recipient already
> has an unexpired channel in the cache, which the startup config call prefills
> from `publicChannels` — that map carries the current user's own channel, so a
> send addressed to yourself often goes out with no lookup and no rejection. It
> is still not a delivery receipt, and what comes back arrives on a
> `SubscriptionType.Client`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} subscription rather than the
> `Server`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} one `subscribe()`{className="language-ts shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts" style=""} gives you by default.
> `sendMessageToChannels()` takes channel ids from the caller, so it never makes
> that lookup and never raises that code.
> Both reject with `JSSDK_PULL_SEND_REFUSED`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""} when the transport
> will not take the frame, and `JSSDK_PULL_PUBLISHING_DISABLED`{className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style=""}
> when the portal has not enabled client publishing. Until 3.0.0 all of these
> resolved as though the message had been sent — see
> [the migration note](https://b24-js-sdk.bx-shef.by/raw/docs/getting-started/migration/within-2x.md#300--%D0%BE%D1%82%D0%BF%D1%80%D0%B0%D0%B2%D0%BA%D0%B0-%D1%87%D0%B5%D1%80%D0%B5%D0%B7-pullclient-%D1%81%D0%BE%D0%BE%D0%B1%D1%89%D0%B0%D0%B5%D1%82-%D0%BE%D0%B1-%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D0%B0%D1%85-%D0%B0-%D0%BD%D0%B5-%D1%81%D0%BA%D1%80%D1%8B%D0%B2%D0%B0%D0%B5%D1%82-%D0%B8%D1%85).

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
