Skip navigation.

CometD 2 & Multiple Sessions

CometD 2 & Multiple Sessions

The HTTP protocol recommends a connection limit of two connections per domain.
While modern browsers are configured by default with more than two connections per domain, it is not safe to make such assumption; thus any iframes, tabs or windows on the same browser connecting to the same host need to share two connections.

If two iframes/tabs/windows initiate a Bayeux communication, both will start a long poll connect request and both connections will be consumed, making impossible to send another Bayeux request (for example a publish) until one of the two long polls returns.

The CometD Server implements the multiple-clients advice specified in the Bayeux specification.
The server uses BAYEUX_BROWSER cookie to detect multiple CometD clients from the same browser.

If multiple clients are detected, then only one long poll connection is allowed and subsequent long poll requests will not wait for messages before returning. They will return immediately with the multiple-clients field of the advice object set to true. The advice will also contain an interval field set to the value of the multiSessionInterval servlet init parameter (see here). This instructs the client not to send another poll until that interval has passed.

The effect of this advice is that additional client connections will normal poll the server with a period of multiSessionInterval. This avoids consume both HTTP connections at the cost of some latency for the additional iframes/tabs/windows.

It is recommended that the client application monitor the /meta/connect channel for multiple-clients field in the advice. If detected, the application may ask the user to close the additional tabs, or it could automatically close them or take some other action.

Non-browser clients (or browsers with cookies disabled) must handle the BAYEUX_BROWSER cookie with the same semantic of browsers, or configure the server to allow multiple sessions even without BAYEUX_BROWSER information via the allowMultiSessionsNoBrowser servlet init parameter (see here).