docs.rodeo

MDN Web Docs mirror

XRSystem: requestSession() method

{{APIRef("WebXR Device API")}} {{SeeCompatTable}} {{SecureContext_Header}} 

The {{domxref("XRSystem")}}  interface’s requestSession() method returns a {{jsxref("promise")}}  which resolves to an {{domxref("XRSession")}}  object through which you can manage the requested type of WebXR session.

While only one immersive VR session can be active at a time, multiple inline sessions can be in progress at once.

Syntax

requestSession(mode)
requestSession(mode, options)

Parameters

Return value

A {{jsxref("Promise")}}  that resolves with an {{domxref("XRSession")}}  object if the device and user agent support the requested mode and features.

Exceptions

This method doesn’t throw true exceptions; instead, it rejects the returned promise, passing into it a {{domxref("DOMException")}}  whose name is one of the following:

Session features

The following session features and reference spaces can be requested, either as optionalFeatures or requiredFeatures.

Security

Several session features and the various reference spaces have minimum security and privacy requirements, like asking for user consent and/or requiring the {{HTTPHeader("Permissions-Policy")}} : xr-spatial-tracking directive to be set. See also Permissions and security for more details.

Session feature User consent requirement Permissions policy requirement
bounded-floor Always required xr-spatial-tracking
depth-sensing xr-spatial-tracking
hand-tracking Always required
hit-test xr-spatial-tracking
local Always required for inline sessions xr-spatial-tracking
local-floor Always required xr-spatial-tracking
unbounded Always required xr-spatial-tracking
viewer Always required

See also transient user activation.

Examples

Creating an immersive VR session

The following example calls requestSession() requesting an "immersive-vr" session. If the {{jsxref("Promise")}}  resolves, it sets up a session and initiates the animation loop.

navigator.xr
  .requestSession("immersive-vr")
  .then((xrSession) => {
    xrSession.addEventListener("end", onXRSessionEnded);
    // Do necessary session setup here.
    // Begin the session's animation loop.
    xrSession.requestAnimationFrame(onXRAnimationFrame);
  })
  .catch((error) => {
    // "immersive-vr" sessions are not supported
    console.error(
      "'immersive-vr' isn't supported, or an error occurred activating VR!",
    );
  });

Verifying WebXR support and using a button to start VR mode

The following example shows how to use both isSessionSupported() and requestSession(). First, it checks to see if WebXR is available by verifying the existence of {{domxref("navigator.xr")}} . Next, it calls isSessionSupported(), passing it the desired session option before enabling controls for entering XR. Adding controls is a necessary step because entering XR requires a user action. Finally, the onButtonClicked() method calls requestSession() using the same session option passed to isSessionSupported().

if (navigator.xr) {
  navigator.xr.isSessionSupported("immersive-vr").then((isSupported) => {
    if (isSupported) {
      immersiveButton.addEventListener("click", onButtonClicked);
      immersiveButton.textContent = "Enter XR";
      immersiveButton.disabled = false;
    } else {
      console.error("WebXR doesn't support immersive-vr mode!");
    }
  });
} else {
  console.error("WebXR is not available!");
}

function onButtonClicked() {
  if (!xrSession) {
    navigator.xr.requestSession("immersive-vr").then((session) => {
      xrSession = session;
      // onSessionStarted() not shown for reasons of brevity and clarity.
      onSessionStarted(xrSession);
    });
  } else {
    // Button is a toggle button.
    xrSession.end().then(() => (xrSession = null));
  }
}

Requesting a session with required features

Require an unbounded experience in which the user is able to freely move around their physical environment:

navigator.xr.requestSession("immersive-vr", {
  requiredFeatures: ["unbounded"],
});

Requesting a session with a DOM overlay

navigator.xr.requestSession("immersive-ar", {
  optionalFeatures: ["dom-overlay"],
  domOverlay: {
    root: document.getElementById("xr-overlay"),
  },
});

Requesting a depth-sensing session

Here, the caller is able to handle both CPU- and GPU-optimized usage, as well as both “luminance-alpha” and “float32” formats. The order indicates preference for CPU and “luminance-alpha”:

navigator.xr.requestSession("immersive-ar", {
  requiredFeatures: ["depth-sensing"],
  depthSensing: {
    usagePreference: ["cpu-optimized", "gpu-optimized"],
    dataFormatPreference: ["luminance-alpha", "float32"],
  },
});

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN