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

# Long-Poll Events

export const Link = ({href, children, className, ...props}) => {
  const localizedHref = href;
  return <a href={localizedHref} className={className} {...props}>
      {children}
    </a>;
};

export const MultiRelatedLinks = ({sections = []}) => {
  if (!sections || sections.length === 0) {
    return null;
  }
  return <div className="space-y-8">
      {sections.map((section, index) => <RelatedLinks key={index} title={section.title} items={section.items} />)}
    </div>;
};

export const RelatedLinks = ({title, items = []}) => {
  const getBadgeClass = badge => {
    if (!badge) return "badge-default";
    const badgeType = badge.toLowerCase().replace(/\s+/g, "-");
    return `badge-${badge === "ガイド" ? "guide" : badgeType}`;
  };
  if (!items || items.length === 0) {
    return null;
  }
  return <div className="my-8">
      {}
      <h3 className="text-sm font-bold uppercase tracking-wider mb-4">{title}</h3>

      {}
      <div className="flex flex-col gap-3">
        {items.map((item, index) => <a key={index} href={item.href} className="py-2 px-3 rounded related_link hover:bg-[#f2f2f2] dark:hover:bg-[#111827] flex items-center gap-3 group no-underline hover:no-underline border-b-0">
            {}
            <span className={`px-2 py-1 rounded-full text-xs font-semibold uppercase tracking-wide flex-shrink-0 ${getBadgeClass(item.badge)}`}>
              {item.badge}
            </span>

            {}
            <span className="text-base">{item.label}</span>
          </a>)}
      </div>
    </div>;
};

To get real-time notification of activity in a Box account you can use the long
poll feature of the <Link href="/reference/options-events">`OPTIONS /events`</Link> API.

<CodeGroup>
  ```sh cURL theme={null}
  curl -i -X OPTIONS "https://api.box.com/2.0/events" \
       -H "authorization: Bearer <ACCESS_TOKEN>"
  ```

  ```typescript Node/TypeScript v10 theme={null}
  await client.events.getEventsWithLongPolling();
  ```

  ```python Python v10 theme={null}
  client.events.get_events_with_long_polling()
  ```

  ```cs .NET v10 theme={null}
  await client.Events.GetEventsWithLongPollingAsync();
  ```

  ```swift Swift v10 theme={null}
  try await client.events.getEventsWithLongPolling()
  ```

  ```java Java v10 theme={null}
  client.getEvents().getEventsWithLongPolling()
  ```

  ```python Python v4 theme={null}
  events = client.events().generate_events_with_long_polling()
  for event in events:
      print(f'Got {event.event_type} event')
  ```

  ```javascript Node v4 theme={null}
  client.events.getEventStream(function(err, stream) {
    if (err) {
      // handle error
    }
    stream.on('data', function(event) {
      // handle the event
    });
  });
  ```
</CodeGroup>

<Warning>
  Long polling is only available for user events. Enterprise events do not
  support long polling.
</Warning>

## Long Polling

Long polling involves opening an HTTP request and keeping it open until the
server sends a response, then repeating the process over and over to receive
updated responses.

<Info>
  The SDKs have built-in support for turning the event feeds into an event
  stream by long polling for new events.
</Info>

### Long Poll URL

To use long polling, first send an request to the
<Link href="/reference/options-events">`OPTIONS /events`</Link> API to retrieve the long poll URL.

```sh theme={null}
curl -X OPTIONS https://api.box.com/2.0/events \
    -H "authorization: Bearer ACCESS_TOKEN"
```

```json theme={null}
{
  "chunk_size": 1,
  "entries": [
    {
      "type": "realtime_server",
      "url": "http://2.realtime.services.box.net/subscribe?channel=cc807c9c4869ffb1c81a&stream_type=all",
      "ttl": 10,
      "max_retries": 10,
      "retry_timeout": 610
    }
  ]
}
```

### Real-Time Servers

Next, make a `GET` request to the provided URL to begin listening for events. If
an event occurs in an account that is being monitored the application will
receive a response with the value `new_change`. The response contains no other
details.

This single response serves as a prompt to take further action such as
sending a request to the `GET /events` endpoint with the last known
`stream_position`.

### Disconnect & Reconnect

After the server sends this response it closes the connection. The application
must now repeat the long poll process to begin listening for events again.

If no events occur for a while after the application connects to the real-time
server the connection will close with a `reconnect` value. When this happens the
application should make a new call to `OPTIONS /events` to restart the process.

### Timeouts & Retries

If the application receive no events in `retry_timeout` seconds then the
application can reconnect to the real-time server. This might be necessary in
due to network errors.

If the application receive a `max_retries` error when making a `GET` request to
the real-time server then it must restart the process by making an `OPTIONS` call
to the `/events` API.

<RelatedLinks
  title="RELATED APIS"
  items={[
{ label: translate("List user and enterprise events"), href: "/reference/get-events", badge: "GET" },
{ label: translate("Get events long poll endpoint"), href: "/reference/options-events", badge: "OPTIONS" }
]}
/>

<RelatedLinks
  title="RELATED GUIDES"
  items={[
{ label: translate("Get Enterprise Events"), href: "/guides/events/enterprise-events/for-enterprise", badge: "GUIDE" },
{ label: translate("Get User Events"), href: "/guides/events/user-events/for-user", badge: "GUIDE" }
]}
/>
