Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish message #117

Merged
merged 20 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ To instantiate the Chat SDK, create an [Ably client](https://ably.com/docs/getti

```ts
import Chat from '@ably/chat';
import { Realtime } from 'ably';
import * as Ably from 'ably';

const ably = new Realtime.Promise({ key: "<API-key>", clientId: "<client-ID>" });
const ably = new Ably.Realtime({ key: "<API-key>", clientId: "<client-ID>", useBinaryProtocol: false });
const chat = new Chat(ably);
```
You can use [basic authentication](https://ably.com/docs/auth/basic) i.e. the API Key directly for testing purposes, however it is strongly recommended that you use [token authentication](https://ably.com/docs/auth/token) in production environments.
Expand Down Expand Up @@ -146,4 +146,4 @@ room.channel.on('attached', (stateChange) => {
You can also get the realtime channel name of the chat room with
```ts
room.channelName
```
```
22 changes: 18 additions & 4 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
# Chat SDK Demo

An app showcasing the usage of Chat SDK to build chat
An app showcasing the usage of Chat SDK to build chat.

Quickstart install-to-browser, from this folder run:

```
(cd .. && npm run build)
npm install
npm run start
```

## Installation

1. First of all, you need to build the main Chat SDK from the root directory.
2. Run `npm install`.
1. First of all, you need to build the main Chat SDK from the root directory (`(cd .. && npm run build)`).
2. Run `npm install` here.

## Credential Setup

1. Copy `.env.example` to `.env.` and set the `VITE_ABLY_CHAT_API_KEY` to your Ably API key.
2. If you're using a custom Ably domain, also set the `VITE_ABLY_HOST` to your custom domain.

For Ably: if running local realtime, see `src/main.tsx`.

## Running

Run `npm run dev`
Run `npm run start`, and it will automatically open your browser on port 8888.

Use `npm run start-silent` if you'd rather not have your browser open automatically.

`npm run start` (and the `start-silent` version) will run both the API component for generating tokens and the front-end side. If you'd like to only run the front-end site use `npm run dev`.
2 changes: 1 addition & 1 deletion demo/api/ably-token-request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Please see README.md for more details on configuring your Ably API Key.`);
});
const tokenRequestData = await client.auth.createTokenRequest({
capability: {
'room:*': ['publish', 'subscribe', 'presence'],
'[chat]*': ['*'],
'*': ['*'],
},
clientId: clientId,
});
Expand Down
2 changes: 1 addition & 1 deletion demo/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
command = "npm run build"

[functions]
directory = "api"
directory = "demo/api"
external_node_modules = ["express"]
node_bundler = "esbuild"

Expand Down
6 changes: 1 addition & 5 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"prepare": "cd api/ably-token-request && npm install",
"dev": "vite",
"start": "npx netlify dev",
"start-silent": "npx netlify dev --no-open",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
Expand Down
1 change: 1 addition & 0 deletions demo/src/components/MessageInput/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const MessageInput: FC<MessageInputProps> = ({ value, disabled, onValueCh
disabled={disabled}
placeholder="Type.."
className="w-full focus:outline-none focus:placeholder-gray-400 text-gray-600 placeholder-gray-600 pl-2 bg-gray-200 rounded-md py-1"
autoFocus
/>
<div className="absolute right-0 items-center inset-y-0 hidden sm:flex">
<button
Expand Down
33 changes: 18 additions & 15 deletions demo/src/hooks/useMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Message, MessageEvents, type MessageListener } from '@ably-labs/chat';
import { useCallback, useEffect, useState } from 'react';
import { useRoom } from './useRoom';

const combineMessages = (previousMessages: Message[], lastMessages: Message[]) => {
return [...previousMessages.filter((msg) => lastMessages.every(({ id }) => id !== msg.id)), ...lastMessages];
};
// todo: uncomment. used for history query when we add it back
// const combineMessages = (previousMessages: Message[], lastMessages: Message[]) => {
// return [...previousMessages.filter((msg) => lastMessages.every(({ id }) => id !== msg.id)), ...lastMessages];
// };

export const useMessages = () => {
const [messages, setMessages] = useState<Message[]>([]);
Expand All @@ -19,26 +20,28 @@ export const useMessages = () => {
);

useEffect(() => {
setLoading(true);
setLoading(false); // todo: set to true here when we add back history query (see commented initMessages below)

const handleAdd: MessageListener = ({ message }) => {
setMessages((prevMessage) => [...prevMessage, message]);
};

room.messages.subscribe(MessageEvents.created, handleAdd);

let mounted = true;
const initMessages = async () => {
const lastMessages = await room.messages.query({ limit: 100 });
if (mounted) {
setLoading(false);
setMessages((prevMessages) => combineMessages(prevMessages, lastMessages).reverse());
}
};
setMessages([]);
initMessages();

// let mounted = true;
// const initMessages = async () => {
// const lastMessages = await room.messages.query({ limit: 100 });
// if (mounted) {
// // setLoading(false);
// setMessages((prevMessages) => combineMessages(prevMessages, lastMessages).reverse());
// }
// };
// initMessages();


return () => {
mounted = false;
// mounted = false;
room.messages.unsubscribe(MessageEvents.created, handleAdd);
};
}, [clientId, room]);
Expand Down
12 changes: 11 additions & 1 deletion demo/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ import './index.css';

const clientId = nanoid();

// use this for local development with local realtime
//
// const ablyClient = new Ably.Realtime({
// authUrl: `/api/ably-token-request?clientId=${clientId}`,
// port: 8081,
// environment: 'local',
// tls: false,
// clientId,
// });

const ablyClient = new Ably.Realtime({
authUrl: `/api/ably-token-request?clientId=${clientId}`,
restHost: import.meta.env.VITE_ABLY_HOST,
realtimeHost: import.meta.env.VITE_ABLY_HOST,
clientId,
});
})

const chatClient = new Chat(ablyClient);

Expand Down
Loading
Loading