Creates a new DrawbridgeConnection and immediately opens a WebSocket to the server.
The underlying socket uses an auto-reconnect strategy, so transient network interruptions are handled transparently. The connection URL is derived from the provided screen name.
Connection configuration.
Unique identifier for this screen as registered in the Drawbridge project and in playbook settings. Must exactly match the name configured on the server.
The identifier of the most recently received screen, or null if no screen-change
command has been received since the connection was opened.
This value is updated synchronously each time an onScreenChange message arrives, so
it always reflects the last known server state. It is safe to read at any time to
determine the current screen without waiting for the next event.
Whether the WebSocket connection to the Drawbridge server is currently open.
Returns true only when the socket's readyState is OPEN. During the initial
connection attempt, a reconnect pause, or after close has been called, this
will be false. Calls to setVariable while disconnected will be queued by the
underlying ReconnectingWebSocket and sent once the link is restored.
Adds an event listener to the connection's internal EventTarget.
This follows the standard EventTarget.addEventListener interface and can be used to subscribe to custom events dispatched internally by the SDK, or to fan out connection events to multiple listeners without replacing the primary onScreenChange callback.
A case-sensitive string identifying the event type to listen for.
The handler invoked when a matching event is dispatched.
Optionaloptions: boolean | AddEventListenerOptionsAn AddEventListenerOptions object or a boolean useCapture flag,
forwarded directly to the underlying EventTarget.
Permanently closes the WebSocket connection to the Drawbridge server.
After calling close, the auto-reconnect behaviour is disabled and no further messages
will be sent or received. This should be called when the screen is unmounted or the
page is about to unload to avoid leaving orphaned sockets on the server.
Registers a callback that fires whenever the server sends a screen-change command.
Only one callback is active at a time — calling onScreenChange again replaces the
previous registration. The callback receives the new screen identifier as a string,
which maps to a state defined in the Drawbridge project (e.g. "0", "1", "intro").
The callback also fires on reconnect if the server re-sends the current screen state.
Function invoked with the new screen identifier each time the active screen changes. The string value is defined by the Drawbridge project configuration.
Removes an event listener previously registered via addEventListener.
The type, listener, and options arguments must match those used when the listener
was added, following the same semantics as
EventTarget.removeEventListener.
The event type string that was passed to addEventListener.
The exact listener reference that was registered.
Optionaloptions: boolean | EventListenerOptionsThe capture flag or options object that was passed when adding the listener.
Sends a variable update to the Drawbridge server.
Variables are the primary mechanism for a screen to report state back to the server — for example, signalling that a puzzle has been solved, submitting an answer, or reporting a numeric score. The server can then use these values to trigger transitions, unlock other screens, or drive game logic.
If the connection is not currently open, the underlying ReconnectingWebSocket will
buffer the message and deliver it once the link is restored.
The name of the variable as defined in the Drawbridge playbook settings. This must match the variable name configured on the server exactly (case-sensitive).
The new value for the variable. Accepts string, number, or boolean.
Manages a persistent WebSocket connection between a screen client and the Drawbridge server.
Each instance represents one named screen (display panel) in a Drawbridge experience. The connection is established automatically on construction and will reconnect if the server drops the link. Incoming server commands are dispatched to registered callbacks, and outgoing variable updates are sent back over the same socket.
Example
Example