In most cases, you will already have an existing application that you want to add chat functionality to.
In this tutorial, we will build a simple delivery app first to demonstrate how to integrate UIKit into an existing app. This app requires a chat feature so that users can communicate with their delivery person.
To get started, clone the starter code from the GitHub repository.
You can find the code in the integrate-with-existing-app/before folder.
The app basically has two screens as follows:
Login Screen: A simple form to enter the user ID.
Order Status Screen: A screen to show the order details and the status.
We will define a custom hook to create a channel and open the chat screen.
const useCreateChannel = (userId: string, deliveryPersonId: string, onCreateChannel: (url: string) => void) => {
// To use the context, the component should be wrapped in the SendbirdProvider.
const { stores } = useSendbirdStateContext(); // Access the Sendbird state context
const sdk = stores.sdkStore.sdk; // Get the Sendbird SDK instance
return async () => {
// Ensure that SDK is initialized
if (!sdk || !sdk.groupChannel) {
console.log('SDK not initialized');
return;
}
// Use the SDK to create a new group channel
const params = {
invitedUserIds: [userId, deliveryPersonId],
isDistinct: true, // Reuse the channel if it already exists
};
// In production, you should handle the error using try-catch
const channel = await sdk.groupChannel.createChannel(params);
onCreateChannel(channel.url);
};
};
In the code snippet above:
Using the sdk instance, you can create a channel, send a message, and more. For more information, please refer to the API reference page.
The SendbirdProvider is a context provider that provides the Sendbird SDK instance to its children. It should be placed at the top of the component tree to provide the SDK instance to all the children components.