Skip to content

Commit

Permalink
docs: review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ttypic committed Nov 23, 2023
1 parent 2be4b7d commit 3c849b3
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,18 @@ A Conversation is a chat between one or more participants that may be backed by
const conversation = await client.create(`namespace:${entityId}`);
```

## Listen to changes
## Getting existing Conversation

### Listen to all changes
You can connect to the existing conversation by its name:

```ts
conversation.subscribe(({ type, ...payload }) => {
switch (type) {
case 'message.created':
case 'message.updated':
case 'message.deleted':
console.log('messages event', payload);
break;
case 'reaction.added':
case 'reaction.deleted':
console.log('messages event', payload);
break;
case 'members.enter':
case 'members.leave':
case 'members.remove':
case 'members.updateProfile':
console.log('members event', payload);
break;
case 'typings.typed':
case 'typings.stopped':
console.log('members event', payload);
break;
}
});
const conversation = await client.get(`namespace:${entityId}`);
```

Also you can send `createIfNotExists: true` option that will create new Conversation if it doesn't exist.

```ts
const conversation = await client.get(`namespace:${entityId}`, { createIfNotExists: true });
```

## Messaging
Expand Down Expand Up @@ -152,13 +136,51 @@ conversation.messages.subscribe(({ type, message, reaction, diff, messageId, rea
});
```

```ts
// Subscribe to specific even in a conversation
conversation.messages.subscribe('message.created', ({ type, message }) => {
console.log(message);
});
```

### Subscribe and fetch latest messages

Common use-case for Messages is getting latest messages and subscribe to future updates, to make it easier,
you can use `fetch` option:

```ts
conversation.messages.subscribe(({ type, messages, ...restEventsPayload }) => {
switch (type) {
case 'messages.fetched':
console.log(messages);
default:
console.log(type, restEventsPayload);
}
}, {
fetch: {
limit,
from,
to,
direction,
}
});
```

[//]: # (TODO message statuses updates: sent, delivered, read)

## Presence

> [!IMPORTANT]
> Idea is to keep it similar to Spaces members and potentially reuse code
```ts
// Enter a conversation, publishing an update event, including optional profile data
await conversation.enter({
username: 'Claire Lemons',
avatar: 'https://slides-internal.com/users/clemons.png',
});
```

```ts
// Subscribe to all member events in a conversation
conversation.members.subscribe((memberUpdate) => {
Expand All @@ -184,11 +206,6 @@ conversation.members.subscribe('remove', (memberRemoved) => {
conversation.members.subscribe('updateProfile', (memberProfileUpdated) => {
console.log(memberProfileUpdated);
});

// Subscribe to all updates to members
conversation.members.subscribe('update', (memberUpdate) => {
console.log(memberUpdate);
});
```

### Getting a snapshot of members
Expand Down

0 comments on commit 3c849b3

Please sign in to comment.