Skip to content

Commit 6dd273b

Browse files
authored
feat: add internal polling inbound transporter (#323)
1 parent f2e3a06 commit 6dd273b

File tree

7 files changed

+64
-93
lines changed

7 files changed

+64
-93
lines changed

docs/getting-started/1-transports.md

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,65 +27,22 @@ agent.setOutboundTransporter(wsOutboundTransporter)
2727

2828
## Inbound Transport
2929

30-
Inbound transports allow you to receive messages from other agents. No inbound transports are exported from the framework at the moment. See the example transports below to get started.
30+
Inbound transports allow you to receive messages from other agents. Only `PollingInboundTransporter` is exported from the framework at the moment. Make sure you provide a `mediatorUrl` if using the polling inbound transporters. See the example transports below for other inbound transports.
3131

3232
```ts
33+
import { Agent, PollingInboundTransporter } from 'aries-framework'
34+
3335
const agentConfig = {
3436
// ... agent config ... //
37+
mediatorUrl: 'https://your-afj-mediator-url.com',
3538
}
3639

37-
const someInboundTransport = new SomeInboundTransport()
38-
3940
const agent = new Agent(agentConfig)
40-
agent.setInboundTransporter(someInboundTransport)
41-
```
42-
43-
### Example `PollingInboundTransporter`
44-
45-
This is an example of a polling inbound transport. This is mostly useful for edge agents, that use a mediator to receive inbound messages. Make sure to provide a `mediatorUrl` in the agent config when using this transport. See [3. Routing](./3-routing.md) for more information.
4641

47-
```ts
48-
import { Agent, InboundTransporter } from 'aries-framework'
49-
50-
// In React Native you don't have to import node-fetch
51-
// Fetch is globally available in React Native
52-
import fetch from 'node-fetch'
53-
54-
class PollingInboundTransporter implements InboundTransporter {
55-
public stop: boolean
42+
// Construct polling inbound transporter with optional polling interval in ms
43+
const pollingInboundTransporter = new PollingInboundTransporter(5000)
5644

57-
public constructor() {
58-
this.stop = false
59-
}
60-
public async start(agent: Agent) {
61-
await this.registerMediator(agent)
62-
}
63-
64-
public async registerMediator(agent: Agent) {
65-
const mediatorUrl = agent.getMediatorUrl()
66-
67-
if (!mediatorUrl) {
68-
throw new AriesFrameworkError(
69-
'Agent has no mediator URL. Make sure to provide the `mediatorUrl` in the agent config.'
70-
)
71-
}
72-
73-
const mediatorInvitationUrl = await get(`${mediatorUrl}/invitation`)
74-
const { verkey: mediatorVerkey } = JSON.parse(await get(`${mediatorUrl}/`))
75-
await agent.routing.provision({
76-
verkey: mediatorVerkey,
77-
invitationUrl: mediatorInvitationUrl,
78-
})
79-
this.pollDownloadMessages(agent)
80-
}
81-
82-
private async pollDownloadMessages(agent: Agent) {
83-
while (!this.stop) {
84-
await agent.routing.downloadMessages()
85-
await sleep(5000)
86-
}
87-
}
88-
}
45+
agent.setInboundTransporter(pollingInboundTransporter)
8946
```
9047

9148
### Example `HttpInboundTransporter`

src/__tests__/helpers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ export const genesisPath = process.env.GENESIS_TXN_PATH
3333

3434
export const publicDidSeed = process.env.TEST_AGENT_PUBLIC_DID_SEED ?? '000000000000000000000000Trustee9'
3535

36-
export const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms))
37-
3836
export function getBaseConfig(name: string, extraConfig: Partial<InitConfig> = {}) {
3937
const config: InitConfig = {
4038
label: `Agent: ${name}`,

src/__tests__/ledger.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import indy from 'indy-sdk'
55

66
import { Agent } from '../agent/Agent'
77
import { DID_IDENTIFIER_REGEX, VERKEY_REGEX, isFullVerkey, isAbbreviatedVerkey } from '../utils/did'
8+
import { sleep } from '../utils/sleep'
89

9-
import { genesisPath, getBaseConfig, sleep } from './helpers'
10+
import { genesisPath, getBaseConfig } from './helpers'
1011
import testLogger from './logger'
1112

1213
const faberConfig = getBaseConfig('Faber Ledger', { genesisPath })
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { Agent } from '../agent/Agent'
2+
import type { InboundTransporter } from './InboundTransporter'
3+
4+
import { AriesFrameworkError } from '../error/AriesFrameworkError'
5+
import { fetch } from '../utils/fetch'
6+
import { sleep } from '../utils/sleep'
7+
8+
export class PollingInboundTransporter implements InboundTransporter {
9+
public stop: boolean
10+
private pollingInterval: number
11+
12+
public constructor(pollingInterval = 5000) {
13+
this.stop = false
14+
this.pollingInterval = pollingInterval
15+
}
16+
17+
public async start(agent: Agent) {
18+
await this.registerMediator(agent)
19+
}
20+
21+
public async registerMediator(agent: Agent) {
22+
const mediatorUrl = agent.getMediatorUrl()
23+
24+
if (!mediatorUrl) {
25+
throw new AriesFrameworkError(
26+
'Agent has no mediator URL. Make sure to provide the `mediatorUrl` in the agent config.'
27+
)
28+
}
29+
30+
const invitationResponse = await fetch(`${mediatorUrl}/invitation`)
31+
const invitationUrl = await invitationResponse.text()
32+
33+
const mediatorDidResponse = await fetch(`${mediatorUrl}`)
34+
const { verkey } = await mediatorDidResponse.json()
35+
36+
await agent.routing.provision({
37+
verkey,
38+
invitationUrl,
39+
})
40+
this.pollDownloadMessages(agent)
41+
}
42+
43+
private async pollDownloadMessages(agent: Agent) {
44+
while (!this.stop) {
45+
await agent.routing.downloadMessages()
46+
await sleep(this.pollingInterval)
47+
}
48+
}
49+
}

src/transport/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './InboundTransporter'
22
export * from './OutboundTransporter'
33
export * from './HttpOutboundTransporter'
44
export * from './WsOutboundTransporter'
5+
export * from './PollingInboundTransporter'

src/utils/sleep.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function sleep(ms: number) {
2+
return new Promise((res) => setTimeout(res, ms))
3+
}

tests/__tests__/e2e.test.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import type { InboundTransporter } from '../../src'
2-
3-
import { Agent, AriesFrameworkError, HttpOutboundTransporter } from '../../src'
4-
import { getBaseConfig, sleep, waitForBasicMessage } from '../../src/__tests__/helpers'
1+
import { Agent, HttpOutboundTransporter, PollingInboundTransporter } from '../../src'
2+
import { getBaseConfig, waitForBasicMessage } from '../../src/__tests__/helpers'
53
import logger from '../../src/__tests__/logger'
64
import { get } from '../http'
75

@@ -93,39 +91,3 @@ describe('with mediator', () => {
9391
expect(basicMessage.content).toBe(message)
9492
})
9593
})
96-
97-
class PollingInboundTransporter implements InboundTransporter {
98-
public stop: boolean
99-
100-
public constructor() {
101-
this.stop = false
102-
}
103-
public async start(agent: Agent) {
104-
await this.registerMediator(agent)
105-
}
106-
107-
public async registerMediator(agent: Agent) {
108-
const mediatorUrl = agent.getMediatorUrl()
109-
110-
if (!mediatorUrl) {
111-
throw new AriesFrameworkError(
112-
'Agent has no mediator URL. Make sure to provide the `mediatorUrl` in the agent config.'
113-
)
114-
}
115-
116-
const mediatorInvitationUrl = await get(`${mediatorUrl}/invitation`)
117-
const { verkey: mediatorVerkey } = JSON.parse(await get(`${mediatorUrl}/`))
118-
await agent.routing.provision({
119-
verkey: mediatorVerkey,
120-
invitationUrl: mediatorInvitationUrl,
121-
})
122-
this.pollDownloadMessages(agent)
123-
}
124-
125-
private async pollDownloadMessages(agent: Agent) {
126-
while (!this.stop) {
127-
await agent.routing.downloadMessages()
128-
await sleep(5000)
129-
}
130-
}
131-
}

0 commit comments

Comments
 (0)