Skip to content

Commit

Permalink
docs(messaging): improve messaging service documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Harasz committed May 11, 2022
1 parent 787e848 commit 509e201
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 22 deletions.
58 changes: 43 additions & 15 deletions docs/api/classes/modules_messaging.MessagingService.md
Expand Up @@ -2,6 +2,13 @@

[modules/messaging](../modules/modules_messaging.md).MessagingService

Service responsible for handling the messaging via NATS.

```typescript
const { messagingService } = await initWithPrivateKeySigner(privateKey, rpcUrl);
messagingService.subscribeTo(...);
```

## Table of contents

### Constructors
Expand Down Expand Up @@ -42,48 +49,69 @@ ___

### publish

**publish**(`subject`, `data`): `Promise`<`void`\>
**publish**(`subject`, `data`): `void`

Publish a message with data to the given subject.

```typescript
messagingService.publish('*.*.did:ethr:volta:0x00..0.ewf-volta', Uint8Array.from('Hello World'));
```

#### Parameters

| Name | Type |
| :------ | :------ |
| `subject` | `string` |
| `data` | `Uint8Array` |
| Name | Type | Description |
| :------ | :------ | :------ |
| `subject` | `string` | message subject |
| `data` | `Uint8Array` | message data |

#### Returns

`Promise`<`void`\>
`void`

___

### subscribeTo

**subscribeTo**(`__namedParameters`): `Promise`<`undefined` \| `number`\>
**subscribeTo**(`options`): `Promise`<`undefined` \| `number`\>

Subscribe to messages on the given subject.

```typescript
messagingService.subscribeTo({
subject: '*.*.did:ethr:volta:0x00..0.ewf-volta',
messageHandler: (data) => console.log(data),
});
```

#### Parameters

| Name | Type |
| :------ | :------ |
| `__namedParameters` | `Object` |
| `__namedParameters.subject?` | `string` |
| `__namedParameters.messageHandler` | (`data`: [`IMessage`](../interfaces/modules_messaging.IMessage.md)) => `void` |
| Name | Type | Description |
| :------ | :------ | :------ |
| `options` | [`SubscribeToOptions`](../interfaces/modules_messaging.SubscribeToOptions.md) | object with options |

#### Returns

`Promise`<`undefined` \| `number`\>

subscription id

___

### unsubscribeFrom

**unsubscribeFrom**(`subscriptionId`): `Promise`<`void`\>

Unsubscribe from the given subscription id.

```typescript
messagingService.unsubscribeFrom(55);
```

#### Parameters

| Name | Type |
| :------ | :------ |
| `subscriptionId` | `number` |
| Name | Type | Description |
| :------ | :------ | :------ |
| `subscriptionId` | `number` | subscription id |

#### Returns

Expand Down
22 changes: 22 additions & 0 deletions docs/api/interfaces/modules_messaging.SubscribeToOptions.md
@@ -0,0 +1,22 @@
# Interface: SubscribeToOptions

[modules/messaging](../modules/modules_messaging.md).SubscribeToOptions

## Table of contents

### Properties

- [messageHandler](modules_messaging.SubscribeToOptions.md#messagehandler)
- [subject](modules_messaging.SubscribeToOptions.md#subject)

## Properties

### messageHandler

**messageHandler**: [`MessageHandler`](../modules/modules_messaging.md#messagehandler)

___

### subject

`Optional` **subject**: `string`
25 changes: 25 additions & 0 deletions docs/api/modules/modules_messaging.md
Expand Up @@ -13,3 +13,28 @@
### Interfaces

- [IMessage](../interfaces/modules_messaging.IMessage.md)
- [SubscribeToOptions](../interfaces/modules_messaging.SubscribeToOptions.md)

### Type aliases

- [MessageHandler](modules_messaging.md#messagehandler)

## Type aliases

### MessageHandler

Ƭ **MessageHandler**: (`message`: [`IMessage`](../interfaces/modules_messaging.IMessage.md)) => `void`

#### Type declaration

▸ (`message`): `void`

##### Parameters

| Name | Type |
| :------ | :------ |
| `message` | [`IMessage`](../interfaces/modules_messaging.IMessage.md) |

##### Returns

`void`
52 changes: 45 additions & 7 deletions src/modules/messaging/messaging.service.ts
Expand Up @@ -6,14 +6,26 @@ import {
Subscription,
} from 'nats.ws';
import { getMessagingConfig } from '../../config/messaging.config';
import { IMessage, MessagingMethod } from './messaging.types';
import {
IMessage,
MessagingMethod,
SubscribeToOptions,
} from './messaging.types';
import { SignerService } from '../signer/signer.service';
import {
executionEnvironment,
ExecutionEnvironment,
} from '../../utils/detect-environment';
import { getLogger } from '../../config/logger.config';

/**
* Service responsible for handling the messaging via NATS.
*
* ```typescript
* const { messagingService } = await initWithPrivateKeySigner(privateKey, rpcUrl);
* messagingService.subscribeTo(...);
* ```
*/
export class MessagingService {
private _jsonCodec: Codec<unknown>;
private _natsConnection: NatsConnection;
Expand Down Expand Up @@ -63,13 +75,22 @@ export class MessagingService {
}
}

/**
* Subscribe to messages on the given subject.
*
* ```typescript
* messagingService.subscribeTo({
* subject: '*.*.did:ethr:volta:0x00..0.ewf-volta',
* messageHandler: (data) => console.log(data),
* });
* ```
* @param {SubscribeToOptions} options object with options
* @return subscription id
*/
async subscribeTo({
subject = `*.*.${this._signerService.did}.${this._natsEnvironmentName}`,
messageHandler,
}: {
subject?: string;
messageHandler: (data: IMessage) => void;
}) {
}: SubscribeToOptions): Promise<number | undefined> {
if (!this._natsConnection) {
return;
}
Expand All @@ -87,7 +108,15 @@ export class MessagingService {
return subscription.getID();
}

async unsubscribeFrom(subscriptionId: number) {
/**
* Unsubscribe from the given subscription id.
*
* ```typescript
* messagingService.unsubscribeFrom(55);
* ```
* @param {Number} subscriptionId subscription id
*/
async unsubscribeFrom(subscriptionId: number): Promise<void> {
const i = this._subscriptions.findIndex(
(s) => s.getID() === subscriptionId
);
Expand All @@ -96,7 +125,16 @@ export class MessagingService {
}
}

async publish(subject: string, data: Uint8Array) {
/**
* Publish a message with data to the given subject.
*
* ```typescript
* messagingService.publish('*.*.did:ethr:volta:0x00..0.ewf-volta', Uint8Array.from('Hello World'));
* ```
* @param {String} subject message subject
* @param {Uint8Array} data message data
*/
publish(subject: string, data: Uint8Array): void {
this._natsConnection?.publish(subject, data);
}
}
7 changes: 7 additions & 0 deletions src/modules/messaging/messaging.types.ts
Expand Up @@ -10,3 +10,10 @@ export interface IMessage {
requester: string;
claimIssuer?: string[];
}

export type MessageHandler = (message: IMessage) => void;

export interface SubscribeToOptions {
subject?: string;
messageHandler: MessageHandler;
}

0 comments on commit 509e201

Please sign in to comment.