Add Chat to Your App
Coming soon. The chat packages are in beta: they work end to end in the showcase app and may still change before 1.0.
This recipe gives an app a conversation list and a conversation screen. People message each other one to one or in groups, with photos and files, delivery and read receipts, typing and unread badges. Messages sent offline wait in an outbox and go out in order when the phone is back online.
It runs in the showcase app, under
Plugins > Chat, where
ChatRecipeTest
exercises it.
The pieces
Chat is three free, MIT-licensed packages on top of Firstlight's Chat and Chat Composer controls:
| Package | Runs on | Does |
|---|---|---|
firstlightui/chat |
The phone | Keeps an offline cache of conversations, sends through the outbox, syncs, and fills the controls. |
firstlightui/chat-server |
Your Laravel backend | Stores conversations, messages, cursors and private attachments, broadcasts changes and hands new messages to your push notifier. |
firstlightui/chat-protocol |
Both | The wire format the two share. |
The controls work on their own too: any PHP that produces messages can publish them.
1. Add the server to your backend
composer require "firstlightui/chat-server:^1.0@beta"
php artisan migrate
Routes live under /api/chat behind auth:sanctum. Your app decides who can
talk to whom, so it creates conversations:
use FirstlightUI\ChatServer\Facades\Chat;
$direct = Chat::direct($jo, $sam);
$group = Chat::group('Dinner', [$jo, $sam]);
Push stays with your provider. After each message the server calls a
ChatNotifier with every other member and their unread total; the default
sends a Laravel notification through the channels you configure.
2. Add the plugin to the app
composer require "firstlightui/chat:^1.0@beta"
php artisan native:plugin:register firstlightui/chat
Set CHAT_SERVER_URL to your backend, then tell the plugin who is signed in:
use FirstlightUI\Chat\Facades\Chat;
use Native\Mobile\Facades\SecureStorage;
Chat::authenticateUsing(fn () => SecureStorage::get('api_token'));
Chat::identifyUsing(fn () => SecureStorage::get('user_id'));
Call Chat::forget() on sign-out to clear the cache on the phone.
For real-time messages, receipts and typing, also configure Vibe. Without it, an open conversation syncs every 5 seconds, the list every 10, and nobody sees typing.
3. Build the conversation screen
The ShowsChat concern loads one conversation and handles every event the
controls send:
use FirstlightUI\Chat\Concerns\ShowsChat;
use FirstlightUI\NativeComponent;
class Conversation extends NativeComponent
{
use ShowsChat;
public function mount(string $conversation): void
{
$this->openChat($conversation);
}
public function render()
{
return view('native.conversation');
}
}
<native:column class="w-full flex-1">
<firstlight:chat
class="w-full flex-1"
:messages="$chatMessages"
me="{{ $chatMe }}"
:typing="$chatTyping"
:has-older="$chatHasOlder"
@read="chatRead"
@retry="chatRetry"
@loadOlder="chatLoadOlder"
@openAttachment="chatOpenAttachment"
label="Chat with {{ $chatTitle }}"
/>
<native:bottom-bar>
<firstlight:chat-composer
native:model.debounce.300ms="chatDraft"
:attachments="$chatPending"
attach="photos,camera,files"
placeholder="Message"
@send="chatSend"
@attach="chatAttach"
@removeAttachment="chatRemoveAttachment"
@typing="chatTyped"
label="Message to {{ $chatTitle }}"
/>
</native:bottom-bar>
</native:column>
The ListsConversations concern does the same for the conversation list:
call listConversations() in mount(), render $chatConversations with
List Item, and use $chatTotalUnread for a tab badge.
What to expect
- Screens read only the cache on the phone, so they open at once, even offline. Network calls run in the background.
- A message the server refuses is marked failed, and Try again sends it again.
- Long conversations open at the newest page and load older history as the reader scrolls.
- Replying from a notification on iOS is planned and not in the beta.