Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

92 lines
3.5 KiB
Markdown

# Getting started
Add `@rocket.chat/ddp-client` and `@rocket.chat/emitter` as dependencies of your project:
`yarn add @rocket.chat/ddp-client @rocket.chat/emitter`
or:
`npm install @rocket.chat/ddp-client @rocket.chat/emitter`
> @rocket.chat/emitter is listed as a peer dependency of ddp-client and is strictly necessary to make it work.
> Tip: The whole project is typed using typescript. For that reason, references to interfaces and objects will be kept to a minimum.
## Setting up the SDK
>This works out of the box for browsers. if you want to use it on NodeJS, you need to offer a `WebSocket` implementation and a `fetch` implementation.
First things first, let's import the SDK:
`import { DDPSDK } from '@rocket.chat/ddp-client';`
Now we need to create a new SDK instance. Fortunately, `DDPSDK` exposes a `create` function that initalizes everything for a quick setup:
`const sdk = DDPSDK.create('http://localhost:3000');`
We can then try to connect to the Rocket.Chat instance by doing:
`await sdk.connection.connect();`
You can check the connection status by referencing `sdk.connection.status`. If everything went right, it's value should be `'connected'`.
> If you're feeling fancy, you can create and connect in a single function call:
> `const sdk = DDPSDK.createAndConnect('http://localhost:3000');`
## SDK Modules
### Connection
Responsible for the connection to the server, status and connection states.
### Account
Responsible for the account management, login, logout, handle credentials, get user information, etc.
### ClientStream
Responsible for the DDP communication, method calls, subscriptions, etc.
### TimeoutControl
Responsible for the Reconnection control
### RestClient
Responsible for the REST API communication for more info [see here](https://developer.rocket.chat/reference/api/rest-api)
## Login handling
The just created `sdk` exposes an `account` interface (`sdk.account`), which should have everything you need. It has 3 methods:
1. `sdk.account.loginWithPassword(username, hashedPassword)`
- This method accepts 2 parameters, `username` and `password`. The password must be hashed as `sha-256` for it to work
2. `sdk.account.loginWithToken('userTokenGoesHere')`
- If you already got the token someway through the API, you can call this method.
3. `sdk.account.logout()`
- This one is self-explanatory.
While the `sdk` instance is kept in memory, you can find some user information and credentials by referencing `sdk.account.user`
## REST API
> TIP: You might have to enable CORS in your Rocket.Chat instance for this to work.
The sdk exposes a `rest` interface, which accept all rest methods (`get`, `post`, `put`, `delete`).
Example call:
`await sdk.rest.post('/v1/chat.sendMessage', { message: { rid: id, msg } });`
> WARNING: if you wrap a rest call in a try catch block, the error will be of type `Response`. By calling `error.json()` you get access to the server error response.
## Streams
Rocket.Chat uses websockets as to provide realtime data. You can subscribe to publications in order to listen to data updates.
Below is an example of subscribing to the room-messages publication, which receives message updates from a room:
```ts
const messages = new Map([]);
const stream = sdk.stream('room-messages', roomId, (args) => {
setMessages((messages) => {
messages.set(args._id, args);
return new Map(messages);
});
});
// Stop the stream when you're done
stream.stop();
```
> TIP: always unsubscribe from publications when you're done with them. This saves bandwidth and server resources