Additional configuration is required to support ChatJS in React Native applications. Use amazon-connect-chatjs@^1.5.0
and apply the changes below:
If running ChatJS in mobile React Native environment, override the default network online check:
amazon-connect-websocket-manager.js
depencency will usenavigator.onLine
. Legacy browsers will always returntrue
, but unsupported or mobile runtime will returnnull/undefined
.
/**
* `amazon-connect-websocket-manager.js` depencency will use `navigator.onLine`
* Unsupported or mobile runtime will return `null/undefined` - preventing websocket connections
* Legacy browsers will always return `true` [ref: caniuse.com/netinfo]
*/
const customNetworkStatusUtil = () => {
if (navigator && navigator.hasOwnProperty("onLine")) {
return navigator.onLine;
}
return true;
}
connect.ChatSession.setGlobalConfig({
webSocketManagerConfig: {
isNetworkOnline: customNetworkStatusUtil,
}
});
Extending this, device-native network health checks can be used for React Native applications.
- First, install the
useNetInfo
react hook:
$ npm install --save @react-native-community/netinfo
# source: https://github.com/react-native-netinfo/react-native-netinfo
- Make sure to update permissions, Android requires the following line in
AndroidManifest.xml
: (for SDK version after 23)
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
/>
- Set up the network event listener, and pass custom function to
setGlobalConfig
:
Note: To configure
WebSocketManager
,setGlobalConfig
must be invoked
import ChatSession from "./ChatSession";
import NetInfo from "@react-native-community/netinfo";
import "amazon-connect-chatjs"; // ^1.5.0 - imports global "connect" object
let isOnline = true;
/**
* By default, `isNetworkOnline` will be invoked every 250ms
* Should only current status, and not make `NetInfo.fetch()` call
*
* @return {boolean} returns true if currently connected to network
*/
const customNetworkStatusUtil = () => isOnline;
const ReactNativeChatComponent = (props) => {
/**
* Network event listener native to device
* Will update `isOnline` value asynchronously whenever network calls are made
*/
const unsubscribeNetworkEventListener = NetInfo.addEventListener(state => {
isOnline = state.isConnected;
});
useEffect(() => {
return unsubscribeNetworkEventListener();
}, []);
const initializeChatJS = () => {
// To configure WebSocketManager, setGlobalConfig must be invoked
connect.ChatSession.setGlobalConfig({
// ...
webSocketManagerConfig: {
isNetworkOnline: customNetworkStatusUtil,
}
});
}
// ...
}
- Optionally, this configuration can be dynamically set based on the
Platform
import { Platform } from 'react-native';
const isMobile = Platform.OS === 'ios' || Platform.OS === 'android';
const customNetworkStatusUtil = () => {
if (navigator && navigator.hasOwnProperty("onLine")) {
return navigator.onLine;
}
return true;
}
connect.ChatSession.setGlobalConfig({
// ...
webSocketManagerConfig: {
...(isMobile ? { isNetworkOnline: customNetworkStatusUtil } : {}), // use default behavior for browsers
}
});