Listening for Events
You can listen for SDK events using Upscope.addListener(). Always remove subscriptions on cleanup to avoid memory leaks.
import Upscope from '@upscopeio/react-native-sdk';
Listening for Events
import React, { useEffect } from 'react';
import Upscope from '@upscopeio/react-native-sdk';
function MyComponent() {
useEffect(() => {
const subs = [
Upscope.addListener('connectionStateChanged', ({ state, error }) => {
switch (state) {
case 'inactive':
console.log('Inactive');
break;
case 'connecting':
console.log('Connecting...');
break;
case 'connected':
console.log('Connected');
break;
case 'reconnecting':
console.log('Reconnecting...');
break;
case 'error':
console.log('Error:', error?.message);
break;
}
}),
Upscope.addListener('sessionStarted', ({ agentName }) => {
console.log(`Session started with ${agentName ?? 'an agent'}`);
}),
Upscope.addListener('sessionEnded', ({ reason, error }) => {
switch (reason) {
case 'userStopped':
console.log('User ended session');
break;
case 'agentStopped':
console.log('Agent ended session');
break;
case 'timeout':
console.log('Session timed out');
break;
case 'error':
console.log('Session error:', error?.message);
break;
}
}),
Upscope.addListener('customMessageReceived', ({ message, viewerId }) => {
console.log(`Message from ${viewerId}: ${message}`);
}),
Upscope.addListener('error', ({ code, message }) => {
console.log(`Error: ${code} - ${message}`);
}),
Upscope.addListener('viewerJoined', (viewer) => {
console.log(`Viewer joined: ${viewer.name ?? viewer.id}`);
}),
Upscope.addListener('viewerLeft', ({ viewerId }) => {
console.log(`Viewer left: ${viewerId}`);
}),
Upscope.addListener('viewerCountChanged', ({ count }) => {
console.log(`Viewers: ${count}`);
}),
Upscope.addListener('remoteControlStateChanged', ({ state }) => {
console.log(`Remote control: ${state}`);
}),
Upscope.addListener('fullDeviceSharingStateChanged', ({ state }) => {
console.log(`Full-device sharing: ${state}`);
}),
Upscope.addListener('sessionRequest', ({ requestId, agentName }) => {
// An agent is requesting to start a cobrowsing session. Only fires
// when the config sets customSessionRequestUI: true (replacing the
// native authorization dialog) and authorization is required.
// Pass true to start the session, false to decline.
const userAllowsSession = true;
console.log(`${agentName ?? 'An agent'} requested a session`);
Upscope.respondToSessionRequest(requestId, userAllowsSession);
}),
Upscope.addListener('controlRequest', ({ requestId, agentName }) => {
// An agent is requesting remote control of the device. Only fires
// when the config sets customControlRequestUI: true (replacing the
// native control request prompt).
// Pass true to grant control, false to decline.
const userGrantsControl = true;
console.log(`${agentName ?? 'An agent'} requested remote control`);
Upscope.respondToControlRequest(requestId, userGrantsControl);
}),
Upscope.addListener('fullDeviceRequest', ({ requestId, agentName }) => {
// Only fires when the config sets customFullDeviceRequestUI: true
// (otherwise the SDK proceeds directly to the system prompt).
// Pass true to proceed to the prompt, false to stay in in-app mode.
const userWantsFullDevice = true;
console.log(`${agentName ?? 'An agent'} requested full-device sharing`);
Upscope.respondToFullDeviceRequest(requestId, userWantsFullDevice);
}),
];
return () => subs.forEach((sub) => sub.remove());
}, []);
return null;
}
Event Reference
| Event | Payload | Description |
|---|---|---|
connectionStateChanged | { state, error? } | Called when the connection state changes. error is present when state is "error". |
sessionStarted | { agentName } | A screen sharing session has started. agentName is the agent's display name if available. |
sessionEnded | { reason, error? } | A session has ended. reason indicates why (userStopped, agentStopped, timeout, or error). |
customMessageReceived | { message, viewerId } | A custom message was received from a viewer. |
error | { code, message } | An SDK error occurred. |
viewerJoined | Viewer | A viewer/agent joined the session. The Viewer includes id, name, screen dimensions, and focus state. |
viewerLeft | { viewerId } | A viewer/agent left the session. |
viewerCountChanged | { count } | The total number of active viewers changed. |
remoteControlStateChanged | { state } | Whether an agent currently has remote control of the device (ability to tap and scroll) changed. state is 'inactive' | 'pendingRequest' | 'active'. Independent of the session being active. |
fullDeviceSharingStateChanged | { state } | Whether full-device (entire screen) sharing is currently running changed, as opposed to default in-app screen sharing. state is 'inactive' | 'pendingRequest' | 'active'. |
sessionRequest | { requestId, agentName } | An agent requested to start a cobrowsing session. Only fires when customSessionRequestUI: true is set in the config and requireAuthorizationForSession is enabled; the native authorization dialog is not shown. agentName is the agent's display name if available, otherwise null. Respond with Upscope.respondToSessionRequest(requestId, accept) to start or decline the session — without a listener that responds, session requests stall and the session never starts. |
controlRequest | { requestId, agentName } | An agent requested remote control of the device. Only fires when customControlRequestUI: true is set in the config and requireControlRequest is enabled (note: requireControlRequest defaults to false, so control is otherwise granted without a request); the native control request prompt is not shown. agentName is the agent's display name if available, otherwise null. Respond with Upscope.respondToControlRequest(requestId, accept) to grant or decline control — without a listener that responds, control requests stall. |
fullDeviceRequest | { requestId, agentName } | An agent requested full-device screen sharing, before the system permission prompt appears. Only fires when customFullDeviceRequestUI: true is set in the config — without the flag, the SDK proceeds directly to the system prompt. agentName is the agent's display name if available, otherwise null. Respond with Upscope.respondToFullDeviceRequest(requestId, accept) to allow (continues to the system prompt) or decline (stays in in-app mode) — without a listener that responds, full-device requests stall. |
shortIdChanged | { shortId } | The visitor's short ID was assigned or changed. |
lookupCodeChanged | { lookupCode } | The lookup code was issued or changed. |
Using Hooks Instead
For most use cases, the reactive hooks are simpler than manual event subscriptions. See SDK Functions for details on useConnectionState(), useSessionState(), and other hooks.
