Skip to content

Commit 35072d6

Browse files
committed
Add cursor pagination documentation to README
1 parent 1423e46 commit 35072d6

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Create a Chat application for your multiple Models
3939
- [Remove participants from a conversation](#remove-participants-from-a-conversation)
4040
- [Add participants to a conversation](#add-participants-to-a-conversation)
4141
- [Get messages in a conversation](#get-messages-in-a-conversation)
42+
- [Get messages with cursor pagination](#get-messages-with-cursor-pagination)
4243
- [Get public conversations for discovery](#get-public-conversations-for-discovery)
4344
- [Get recent messages](#get-recent-messages)
4445
- [Get participants in a conversation](#get-participants-in-a-conversation)
@@ -322,6 +323,44 @@ Chat::conversation($conversation)->addParticipants([$participantModel, $particip
322323
Chat::conversation($conversation)->setParticipant($participantModel)->getMessages()
323324
```
324325

326+
#### Get messages with cursor pagination
327+
328+
For real-time chat applications, cursor-based pagination is recommended over offset-based pagination. This prevents duplicate messages when new messages arrive between page loads.
329+
330+
```php
331+
// Get first page
332+
$messages = Chat::conversation($conversation)
333+
->setParticipant($participantModel)
334+
->setCursorPaginationParams([
335+
'perPage' => 25,
336+
'sorting' => 'asc',
337+
])
338+
->getMessagesWithCursor();
339+
340+
// Get next page using cursor from previous response
341+
$nextCursor = $messages->nextCursor()?->encode();
342+
343+
$moreMessages = Chat::conversation($conversation)
344+
->setParticipant($participantModel)
345+
->setCursorPaginationParams([
346+
'perPage' => 25,
347+
'sorting' => 'asc',
348+
'cursor' => $nextCursor,
349+
])
350+
->getMessagesWithCursor();
351+
```
352+
353+
**API Endpoint:** `GET /conversations/{id}/messages-cursor`
354+
355+
Query parameters:
356+
- `participant_id` (required)
357+
- `participant_type` (required)
358+
- `perPage` (optional, default: 25)
359+
- `sorting` (optional: `asc` or `desc`)
360+
- `cursor` (optional - from previous response's `next_cursor`)
361+
362+
The response includes `next_cursor` and `prev_cursor` for navigation.
363+
325364
#### Get user conversations by type
326365

327366
```php
@@ -380,6 +419,8 @@ You don't have to specify all the parameters. If you leave the parameters out, d
380419
`$paginated` above will return `Illuminate\Pagination\LengthAwarePaginator`
381420
To get the `conversations` simply call `$paginated->items()`
382421

422+
> **Tip:** For paginating messages in real-time chat applications, consider using [cursor pagination](#get-messages-with-cursor-pagination) instead. Cursor pagination prevents duplicate messages when new messages arrive between page loads.
423+
383424

384425
#### Get participants in a conversation
385426

0 commit comments

Comments
 (0)