Skip to content

Communicators

Celine Sarafa edited this page May 19, 2023 · 1 revision

Communicators

Communicators are simplistic in nature, they provide one function, no more no less.

The API they follow is:

export interface ExternalCommunicator {
  postToExternalProvider: <TReturn>(
    methodName: string,
    params: unknown,
    target: 'chat' | 'push'
  ) => Promise<TReturn>
}

Where postToExternalProvider generates a promise that does 2 things:

  • Fires a JSONRPC message to the external provider using a function injected into the window, for example in the case of android it is window.android.postMessage.
  • Listens to the JsonRPC Result on the request, when a response is given it resolves the promise.

For example, in the case of AndroidCommunicator:

 public async postToExternalProvider<TReturn>(
    methodName: string,
    params: unknown,
    _: 'chat' | 'push'
  ) {
    return new Promise<TReturn>(resolve => {
      const message = formatJsonRpcRequest(methodName, params)

      const messageListener = (messageResponse: JsonRpcResult<TReturn>) => {
        resolve(messageResponse.result)
      }
      this.emitter.once(message.id.toString(), messageListener)
      if (window.android) {
        window.android.postMessage(JSON.stringify(message))
      }
    })
  }

For any other platform wishing to be an external provider, the same thing needs to be merged into Web3inbox.

Clone this wiki locally