A lightweight set of polyfills that enable Ably encrypted channels to work seamlessly in React Native environments.
npm install @ably/encrypted-channels-react-native-polyfillsor
yarn add @ably/encrypted-channels-react-native-polyfillsImport and initialize the polyfills at the entry point of your React Native application:
import { install } from '@ably/encrypted-channels-react-native-polyfills';
// Initialize polyfills before using Ably
install();This will polyfill the following Web Crypto API methods:
crypto.subtle.importKey()- Import raw AES keyscrypto.subtle.encrypt()- Encrypt data using AES-CBCcrypto.subtle.decrypt()- Decrypt data using AES-CBC
import { install } from '@ably/encrypted-channels-react-native-polyfills';
// Install polyfills first
install();
function App() {
const [encrypted, setEncrypted] = useState<string>("");
const [key, setKey] = useState<Ably.CipherKey | null>(null);
useEffect(() => {
if (key) return;
Ably.Realtime.Crypto.generateRandomKey().then(generatedKey => setKey(generatedKey))
}, [key]);
useEffect(() => {
if (!key) return;
const channel = client.channels.get('encrypted-channel', {
cipher: { key }
});
channel.subscribe(message => {
setEncrypted(message.data.toString())
});
channel.publish('test', "Hello World");
}, [key]);
}