Getting Started
LikeMinds Flutter Chat SDK provides you with a wrapper layer around the built in functionalities to provide a seamless integration experience within a single sitting without much configuration needed out of the box.
Although, we do provide a high level customisation of all the widgets, screens, and flows being used to power the experience that can be tuned according to the look and feel of your existing apps.
There are two main steps, with a few sub-steps, to get it up and running.
Setup the environment
- Generate API key from LikeMinds dashboard.
- You need only one dependency in your application project to get started without any customizations, namely
likeminds_chat_flutter_core
, which can be found here.
Integrate the SDK
- Add
likeminds_chat_flutter_core
dependency to your project’spubspec.yaml
by adding these lines underdependencies
section
likeminds_chat_flutter_core: ^latest #fetches automatically from pub.dev
When integrating the LikeMinds Chat SDK into your application, you have the option to initiate a user session using one of two approaches:
1. With API Key
This approach should used when you want to manage LikeMinds authentication tokens on frontend.
In this case you provide API Key directly to LikeMinds Chat SDK, which will be used to initiate a user session by calling showChatWithApiKey() method from LMChatCore
.
- Setup the
LMChatCore
package in the main function with the following code.
main(){
// Call setup function before the runApp() function
await LMChatCore.instance.initialize();
...
runApp(YourApp());
}
- Use the snippet of code below to be able to show chat using your API key, this is also used to login using passed user credentials.
// initiate user session, use the response to check for any errors
LMResponse<void> response = await LMChatCore.instance.showChatWithApiKey(
apiKey : "YOUR_API_KEY",
uuid : "USER_ID",
userName : "USER_NAME",
);
- On successful response of the above snippet you can simply navigate to the
LMChatHomeScreen
, and start using Chat in your app
if (response.success) {
MaterialPageRoute route = MaterialPageRoute(
builder: (context) => const LMChatHomeScreen(),
);
Navigator.pushReplacement(context, route);
}
2. Without API Key
This approach should be used when you want to manage LikeMinds authentication tokens on your backend server.
In this case you eliminate the need to expose your API Key to your client app and your backend server is responsible for calling the initiate API to obtain the accessToken
and refreshToken
which is passed to showChatWithoutApiKey() from LMChatCore
to validate the user session.
- Create a function to get
accessToken
andrefreshToken
from your backend using initiate API
Future<(String, String)> getTokens() async {
...
// implementation
...
return (accessToken, refreshToken);
}
- Set up the
LMChatCore
package in the main function with the following code and passLMChatCoreCallback
, which will be invoked when theaccessToken
andrefreshToken
expire.
LMChatCoreCallback
has two callbacks:
onAccessTokenExpiredAndRefreshed: This callback is triggered when the provided
accessToken
expires and is refreshed internally using therefreshToken
.onRefreshTokenExpired: This callback is triggered when the provided
refreshToken
expires. In this case, you need to provide a newaccessToken
andrefreshToken
from your backend function using our initiate API.
main(){
// Call setup function before the runApp() function
await LMChatCore.instance.initialize(
lmChatCallback: LMChatCoreCallback(
onAccessTokenExpiredAndRefreshed: (accessToken, refreshToken) {
debugPrint("Access token expired and refreshed");
},
onRefreshTokenExpired: () async {
// get accessToken, refreshToken from your backend
final (accessToken, refreshToken) = await getTokens();
// return `LMAuthToken` with `accessToken` and `refreshToken` received from your backend
return (LMAuthTokenBuilder()
..accessToken(accessToken!)
..refreshToken(refreshToken!))
.build();
},
),
);
...
runApp(YourApp());
}
- Use the
getTokens()
function, to fetch the tokens to login without API Key. Upon receiving theaccessToken
andrefreshToken
, callLMChatCore.instance.showChatWithoutApiKey()
function with these tokens.
// get accessToken, refreshToken from your backend
final (accessToken, refreshToken) = await getTokens();
LMResponse response =
await LMChatCore.instance.showChatWithoutApiKey(
accessToken : "YOUR_ACCESS_TOKEN",
refreshToken : "YOUR_REFRESH_TOKEN",
);
- On successful response of the above snippet you can simply navigate to the
LMChatHomeScreen
, and start using Chat in your app
if (response.success) {
MaterialPageRoute route = MaterialPageRoute(
builder: (context) => const LMChatHomeScreen(),
);
Navigator.pushReplacement(context, route);
}
Now build the app, and call the above navigation function using a button or tab to experience LMChat.
By choosing the appropriate method based on your backend infrastructure and security preferences, you can seamlessly integrate the Chat SDK into your application while ensuring secure and efficient session management.