Guides
Add the widget to a React Native app
Show the Iris chat page in a WebView, with an unread badge on your own help button.
How it works
Section titled “How it works”Iris hosts a full-screen chat page at https://iris-chat.playitback.dev/. Your app shows it in a WebView from react-native-webview. The chat page opens with the widget already open and no launcher.
- The app and the page talk through the WebView: the page tells the app when Iris is ready, when the contact closes the widget and how many replies are unread, and the app calls
Iris.open,Iris.close,Iris.identify,Iris.logout,Iris.setPageContextandIris.sendMessageon the page. - The page keeps the contact in its own storage, so the contact keeps their conversations after an app restart.
- Fixes to the widget reach your app with no app release.
The chat page URL
Section titled “The chat page URL”Pass your App ID and your workspace’s app key in the query. Copy both from Settings → Install. The page lets the app in only with the app key, and only while the key is on.
https://iris-chat.playitback.dev/?appId=app_7Hq2xK9mPz&appKey=app_key_4f1c9a0b2d7e4c6a8b3f5e1d9c7a2b40The page also reads mode, primary and radius, with the same values as in Iris.init. It reads no other query values.
Add the component
Section titled “Add the component”Install the WebView and the safe area package:
npx expo install react-native-webview react-native-safe-area-contextCopy this component into your app. It keeps the chat page loaded and hidden until the contact opens it.
import { useEffect, useRef, useState } from "react";import { KeyboardAvoidingView, Linking, StyleSheet } from "react-native";import { SafeAreaView } from "react-native-safe-area-context";import { WebView, type WebViewMessageEvent } from "react-native-webview";import type { ShouldStartLoadRequest } from "react-native-webview/lib/WebViewTypes";
type IrisChatMessage = { type: "ready" } | { type: "close" } | { type: "unread"; count: number };
interface IrisChatProps { chatUrl: string; appId: string; appKey: string; isOpen: boolean; identityToken?: string; pageContext?: Record<string, string | number | boolean | null>; onClose: () => void; onUnreadChange: (count: number) => void;}
export const IrisChat = ({ chatUrl, appId, appKey, isOpen, identityToken, pageContext, onClose, onUnreadChange,}: IrisChatProps) => { // react-native-webview 14 types its props as never without a type argument. const webViewRef = useRef<WebView<object>>(null); const pageIdentityToken = useRef<string | undefined>(undefined); const [readyCount, setReadyCount] = useState(0); const pageUrl = `${chatUrl}?${new URLSearchParams({ appId, appKey })}`; const pageContextJson = JSON.stringify(pageContext ?? {});
// injectJavaScript needs a script that ends with a value, so each call ends with "true;". const callIris = (script: string) => { webViewRef.current?.injectJavaScript(`${script}; true;`); };
useEffect(() => { if (readyCount) { callIris(isOpen ? "Iris.open()" : "Iris.close()"); } }, [readyCount, isOpen]);
useEffect(() => { if (!readyCount || identityToken === pageIdentityToken.current) { return; }
callIris( identityToken ? `Iris.identify(${JSON.stringify({ token: identityToken })})` : "Iris.logout()", ); pageIdentityToken.current = identityToken; }, [readyCount, identityToken]);
useEffect(() => { if (readyCount) { callIris(`Iris.setPageContext(${pageContextJson})`); } }, [readyCount, pageContextJson]);
const handleMessage = (event: WebViewMessageEvent) => { const message: IrisChatMessage = JSON.parse(event.nativeEvent.data);
if (message.type === "ready") { pageIdentityToken.current = undefined; setReadyCount((count) => count + 1); }
if (message.type === "close") { onClose(); }
if (message.type === "unread") { onUnreadChange(message.count); } };
const handleNavigation = ({ url }: ShouldStartLoadRequest) => { if (new URL(url).origin === new URL(chatUrl).origin) { return true; }
Linking.openURL(url);
return false; };
return ( <SafeAreaView style={[StyleSheet.absoluteFill, styles.chat, !isOpen && styles.hidden]} pointerEvents={isOpen ? "auto" : "none"} importantForAccessibility={isOpen ? "auto" : "no-hide-descendants"} accessibilityElementsHidden={!isOpen} > <KeyboardAvoidingView style={styles.chat} behavior="padding"> <WebView<object> ref={webViewRef} source={{ uri: pageUrl }} scrollEnabled={false} onMessage={handleMessage} onShouldStartLoadWithRequest={handleNavigation} onOpenWindow={(event) => Linking.openURL(event.nativeEvent.targetUrl)} /> </KeyboardAvoidingView> </SafeAreaView> );};
const styles = StyleSheet.create({ chat: { flex: 1, backgroundColor: "#fff" }, hidden: { opacity: 0 },});Use it on a screen
Section titled “Use it on a screen”Render IrisChat once, on top of your screen, and open it from your own help button. The badge shows the unread count.
const [isChatOpen, setIsChatOpen] = useState(false);const [unreadCount, setUnreadCount] = useState(0);
return ( <SafeAreaProvider> <Pressable onPress={() => setIsChatOpen(true)}> <Text>Help</Text> {!!unreadCount && <Text>{unreadCount}</Text>} </Pressable> <IrisChat chatUrl="https://iris-chat.playitback.dev/" appId="app_7Hq2xK9mPz" appKey="app_key_4f1c9a0b2d7e4c6a8b3f5e1d9c7a2b40" isOpen={isChatOpen} identityToken={user?.irisToken} pageContext={{ screen: "Order details", orderId: order.id }} onClose={() => setIsChatOpen(false)} onUnreadChange={setUnreadCount} /> </SafeAreaProvider>);Messages between the app and the page
Section titled “Messages between the app and the page”The page sends three messages to the app, as JSON strings, through window.ReactNativeWebView.postMessage:
| Message | When |
|---|---|
{ "type": "ready" } |
Once each time the page loads. From then on the app can call Iris. |
{ "type": "close" } |
The contact closes the widget. |
{ "type": "unread", "count": 2 } |
Once when the page loads, then each time the unread count changes. |
The app calls the page with injectJavaScript, for example Iris.open(); true;, only after ready. When the WebView reloads the page, for example after the system stops it in the background, the new page starts signed out and sends ready again: call Iris.identify and Iris.setPageContext again, as the component does. Every method is in Methods. The app cannot wait for the Promise of Iris.sendMessage, because injectJavaScript returns nothing.
Unread badge
Section titled “Unread badge”The page counts agent replies while the widget is closed, and sets the count to 0 when the contact opens it. Keep the WebView mounted and hidden, as the component does, and call Iris.close() when you hide it. A WebView that is not mounted counts nothing.
The count starts at 0 after an app restart, the same as the launcher badge on a website.
Identify users
Section titled “Identify users”Your server signs an identity token for the signed-in user, as for a website. See Identify users. Pass it as identityToken. The component calls Iris.identify when the token arrives and Iris.logout when it goes away.
Page context
Section titled “Page context”The chat page sends no page URL or title of its own. Pass what agents need to know, such as the screen name, as pageContext. The component calls Iris.setPageContext with it.
Links in agent replies open in the phone’s browser, so the chat page never leaves the WebView. The component does this in onShouldStartLoadWithRequest and onOpenWindow.
File uploads
Section titled “File uploads”The contact can attach photos and files. On iOS, add the camera, photo library and microphone texts to your app, or iOS closes the app when the contact picks a photo:
{ "expo": { "ios": { "infoPlist": { "NSCameraUsageDescription": "Take a photo to send to support.", "NSPhotoLibraryUsageDescription": "Choose a photo to send to support.", "NSMicrophoneUsageDescription": "Record a video to send to support." } } }}Android needs no storage permission for uploads.
Replies arrive only while the app is open
Section titled “Replies arrive only while the app is open”Iris sends no push notifications. The contact sees new replies when the app is open, and in the badge while the chat page is hidden.
Turn off or regenerate the key
Section titled “Turn off or regenerate the key”When an admin turns the app key off or regenerates it in Settings → Install, new sessions from your app fail with 403 origin_not_allowed. After you regenerate the key, release an update of your app with the new key. See Troubleshooting.