0.2.15 • Published 19 days ago

@rocket.chat/ddp-client v0.2.15

Weekly downloads
-
License
-
Repository
-
Last release
19 days ago

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

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:

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

0.2.15

19 days ago

0.2.14

2 months ago

0.2.13

3 months ago

0.2.13-rc.7

3 months ago

0.2.13-rc.6

3 months ago

0.2.13-rc.5

3 months ago

0.2.13-rc.2

3 months ago

0.2.13-rc.3

3 months ago

0.2.13-rc.4

3 months ago

0.2.13-rc.0

4 months ago

0.2.13-rc.1

4 months ago

0.2.12

4 months ago

0.2.11

4 months ago

0.2.10

5 months ago

0.2.9

5 months ago

0.2.9-rc.12

5 months ago

0.2.9-rc.11

5 months ago

0.2.9-rc.10

5 months ago

0.2.9-rc.9

5 months ago

0.2.9-rc.8

5 months ago

0.2.9-rc.7

6 months ago

0.2.9-rc.6

6 months ago

0.2.9-rc.5

6 months ago

0.2.9-rc.4

6 months ago

0.2.9-rc.3

6 months ago

0.2.9-rc.2

6 months ago

0.2.9-rc.1

6 months ago

0.2.8

6 months ago

0.2.6-rc.7

6 months ago

0.2.7

6 months ago

0.1.12

6 months ago

0.2.6

6 months ago

0.1.11

6 months ago

0.2.6-rc.6

6 months ago

0.2.6-rc.5

6 months ago

0.2.6-rc.4

6 months ago

0.2.6-rc.3

6 months ago

0.2.6-rc.2

6 months ago

0.2.6-rc.1

6 months ago

0.2.6-rc.0

6 months ago

0.2.5

6 months ago

0.2.4

7 months ago

0.2.3

7 months ago

0.2.2

7 months ago

0.2.1

7 months ago

0.1.10

7 months ago

0.1.9

7 months ago

0.2.0

8 months ago

0.2.0-rc.5

8 months ago

0.1.8

8 months ago

0.2.0-rc.4

8 months ago

0.2.0-rc.3

8 months ago

0.1.7

8 months ago

0.2.0-rc.2

8 months ago

0.2.0-rc.1

8 months ago

0.1.6

8 months ago

0.2.0-rc.0

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0

9 months ago

0.1.0-rc.10

9 months ago

0.0.2-rc.9

10 months ago

0.0.2-rc.8

10 months ago

0.0.2-rc.7

10 months ago

0.0.2-rc.6

10 months ago

0.0.2-rc.5

10 months ago

0.0.2-rc.4

10 months ago

0.0.2-rc.3

10 months ago

0.0.2-rc.2

10 months ago

0.0.2-rc.1

10 months ago

0.0.2-rc.0

10 months ago