Skip to content

Repository files navigation

@podium/bridge

This package is a bridge designed to pass JSON-RPC 2.0 messages between a web application and a native web view.

Usage

To install:

npm install @podium/bridge

Import the bridge in your client-side bundle:

import'@podium/bridge';

You should probably send messages via @podium/browser. That said, the bridge is available on window['@podium'].bridge.

/** @type {import("@podium/bridge").PodiumBridge} */constbridge=window['@podium'].bridge;// You can listen for incoming messages, which can either be RpcRequest or RpcResponsebridge.on('global/authentication',(message)=>{constrequest=/** @type {import("@podium/bridge").RpcRequest<{ token?: string }>} */(message);if(typeofrequest.token==='string'){// logged in}else{// logged out}});// You can trigger notifications (one-way messages)bridge.notification({method: 'global/authentication',params: {token: null},});// And you can call methods and await the response/** @type {import("@podium/bridge").RpcResponse<{ c: string }>} */constresponse=awaitbridge.call({method: 'document/native-feature',params: {a: 'foo',b: 'bar'},});

API

bridge.on

Add a listener for incoming messages for a given method name.

import'@podium/bridge';/** @type {import("@podium/bridge").PodiumBridge} */constbridge=window['@podium'].bridge;bridge.on('global/authentication',(message)=>{constrequest=/** @type {import("@podium/bridge").RpcRequest<{ token?: string }>} */(message);if(typeofrequest.token==='string'){// logged in}else{// logged out}});

bridge.notification

Send a notification (one-way message).

import'@podium/bridge';/** @type {import("@podium/bridge").PodiumBridge} */constbridge=window['@podium'].bridge;bridge.notification({method: 'global/authentication',params: {token: null},});

bridge.call

Send a request and await a response.

import'@podium/bridge';/** @type {import("@podium/bridge").PodiumBridge} */constbridge=window['@podium'].bridge;/** @type {import("@podium/bridge").RpcResponse<{ c: string }>} */constresponse=awaitbridge.call({method: 'document/native-feature',params: {a: 'foo',b: 'bar'},});

About

Bridge between web and native iOS and Android via JSON RPC

Resources

Contributing

Stars

0 stars

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages