locality_social_cloud_chat 1.0.0 copy "locality_social_cloud_chat: ^1.0.0" to clipboard
locality_social_cloud_chat: ^1.0.0 copied to clipboard

A chat based on Locality Social Cloud that supported End-to-End encrypted Chats and GroupChats and Chat Message state management.

Locality Social Cloud Chat #

locality_social_cloud_chat is a Flutter library that provides a real-time messaging system for managing direct messages and group chats. It supports message delivery, reactions, notifications, and invitations, integrating with the locality_social_cloud API.

Features #

  • End-to-End-Encryption: Uses ECDH with elliptic Curve M-511 and ChaCha20 for encryption.
  • Message Reactions: Users can react to messages with predefined or custom reactions.
  • Event-driven Architecture: Respond to specific message types (e.g., text, media) using a PubSub model.
  • Group Chats: Invite users to group chats and manage acceptances.
  • Efficient UI Updates: Implemented using ThrottledChangeNotifier for optimized state updates.
  • Message Observers: Track message states like seen, delivered, and reactions in real-time.

Getting Started #

This project is written with Locality Social Cloud. To use it, you must have a Social Cloud Developer Account and obtain a secret for your app.

Write

flutter pub add locality_social_cloud
flutter pub add locality_social_cloud_chat
flutter pub get
copied to clipboard

to import the package. At some point, register your app secret and log your user in to the Locality Social Cloud.

Configure Locality Social Cloud #

Obtain your app-ID and app secret from your and write

LocalitySocialCloud.configure(app_id: 'YOUR APP ID', app_secret: 'YOUR APP SECRET');
copied to clipboard

Then, run flutter pub get to install the package.

Import the Library #

import 'package:locality_social_cloud_chat/locality_social_cloud_chat.dart';
copied to clipboard

Example Usage #

1. Initialize Direct Messages Chat #

Create an instance of DirectMessagesChat for one-on-one messaging.

DirectMessagesChat chat = await DirectMessagesFacade.getTargetUserChat(
      loggedInUser,
      targetUser
  );
copied to clipboard

2. Sending a Message #

Send a new message to the chat by providing the message payload.

chat.sendMessage({
  'content': 'Hello, this is a message!',
});
copied to clipboard

You can listen to state updates like this:

chat.addListener(() {
    ...
});
copied to clipboard

This is throttled, it means it will at max 120 times per second be called and only if new state is available. You can use it like a Provider to easily integrate with your UI. If you want certain events to only happen after synchronization you can use the Timeline from locality_social_cloud like

chat.timeline.whenSynchronized(() {
    for (var chatMessage in chat.chatMessages) {
      String messageUuid = chatMessage.payload['message_uuid'];
      ....
    }
  });
copied to clipboard

3. Adding a Reaction to a Message #

React to a specific message using its unique message ID.

chat.reactToMessage('message_uuid_123', 'like');
copied to clipboard

4. Tracking Message States #

You can monitor whether a message has been delivered or seen by subscribing to its MessageObserver.

MessageObserver chatMessageObserver = chat.messageObserversByChatMessageUUID[messageUuid]!;
if (!messageObserver.seen) {
    print('Message not seen yet');
}
messageObserver.addListener((){
    // Will be called when the state changes
    if (messageObserver.seen) {
        print('Message seen');
    }
});
copied to clipboard

Use this inside of

chat.timeline.whenSynchronized(() {
    ...
});
copied to clipboard

If you have special message types that should trigger other things like photo_messages triggering a photo download, you could use

chat.whenMessageOfTypeReceived('photo_message', (messageObserver) {
    
});
copied to clipboard

5. Handling Group Chat Invitations #

Invite a user to a group chat or accept an invitation:

chat.inviteToGroupChat('Cool Group', 'group_uuid_456', 'group_topic', BigInt.from(123456));
chat.acceptGroupChatInvitation('message_uuid_789', 'group_uuid_456');
copied to clipboard

Use also an invitation to create a group chat.

The GroupChats class will hold the group chats yielded from accepted invitations.

GroupChats.getInstance().addListener(() {
    List<GroupChat> groupChats = GroupChats.getInstance().groupChats;
    for(GroupChat groupChat in groupChats) {
      print("We are connected to the groupchat ... "+groupChat.title);
    }
  });
copied to clipboard

Event Handling #

The DirectMessagesChat class listens for various chat-related events (message added, reacted, seen, delivered, etc.). You can handle these events by implementing custom actions.

Entering and Leaving a Chat #

When a user enters or leaves a chat, you can influence whether they’ve viewed messages.

chat.enterChat();  // Marks all messages as seen.
chat.leaveChat();  // Marks the chat as unviewed.
copied to clipboard
2
likes
150
points
27
downloads

Publisher

verified publisherlocality.media

Weekly Downloads

2024.06.23 - 2025.01.05

A chat based on Locality Social Cloud that supported End-to-End encrypted Chats and GroupChats and Chat Message state management.

Homepage
Repository
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

crypto, flutter, locality_social_cloud, m511chacha20, theory, uuid

More

Packages that depend on locality_social_cloud_chat