A native <-> browser (webview) bridge library for react-native
npm install lepont

A native <-> browser (webview) bridge library for react-native
Current Version: v0.11.4
> Sous le pont Mirabeau coule la Seine et nos amours -- Guillaume Apollinaire
Note: LePont (le pont) means "the bridge" in French.
You can bridge the webview and react-native by using lepont. You can consider lepont as PhoneGap (Cordova) on top of react-native.
React Native already have large swathe of library ecosystem. You can leverage its power from browser by using lepont 👍
First install it:
``sh`
npm install --save lepontor
yarn add lepont
On react-native side
`tsx
import { useBridge } from 'lepont'
import { WebView } from 'react-native-webview'
type Payload = {
foo: number
}
// This is your bridge implementation
const myBridgeImpl = (payload: Payload) => new Promise((resolve) => {
setTimeout(() => resolve(payload.foo * 2), 300)
})
const App = () => {
// Registers your bridge by the name my-bridge
const [ref, onMessage] = useBridge((registry) => {
registry.register('my-bridge', myBridgeImpl)
})
return (
source={{ uri: 'Web.bundle/index.html' }}
// Needed for sending the message from browser
ref={ref}
// Needed for receiving the message from browser
onMessage={onMessage}
javaScriptEnabled
/>
)
}
export default App
`
Browser side
`ts
import { sendMessage } from 'lepont/browser'
const res = await sendMessage({
type: 'my-bridge',
payload: { foo: 42 }
})
// => res is now 84 after 300ms. It's doubled on react-native side! :)
`
On react-native side
`tsx
import { useBridge } from 'lepont'
import { WebView } from 'react-native-webview'
const App = () => {
const [ref, onMessage] = useBridge((registry) => {
registry.register('my-streaming-bridge', (_, bridge) => {
setInterval(() => {
bridge.sendMessage({
type: 'my-streaming-event',
payload: 'stream data!',
})
}, 1000)
})
})
return (
ref={ref}
onMessage={onMessage}
javaScriptEnabled
/>
)
}
export default App
`
Browser side
`ts
import { sendMessage, on } from 'lepont/browser'
// This triggers the event streaming
sendMessage({ type: 'my-streaming-bridge' })
on('my-streaming-event', (payload) => {
// This fires every second from react-native side! :)
console.log(payload=${payload})`
})
You can package your html and all other assets (css, js) into your app, and we strongly recommend that strategy for reducing significantly the app load time.
See this article for how to bundle the static web assets in your react-native apps.
modulelepont module is for react-native side.
Registers the bridge to the registry by the given BridgeOptions. This returns ref and onMessage of WebView. You need to set these to WebView component to communicate with it.
example:
`tsx
const [ref, onMessage] = useBridge(registry => {
registry.register('my-bridge', MyBridgeImpl)
})
return
`
You can pass BridgeOpion functional option to useBridge hook. In this function you can register your bridge type and implementation through registry.register method.
You can register your bridge type and implementation with this method.
This is the type of bridge implemetation. The 1st param is the payload of your bridge call. The second param is the bridge object. The returned value is serialized and sent back to browser as the result of bridge call. If you like to send back data multiple times to browser you can use bridge.sendMessage method.
Sends the message to browser side. To handle this message, you can register on(type, handler) call on browser side.
modulelepont/browser module is for browser side.
Sends the message to react-native side.
Registers the handler to the event of the given type name.
Unregisters the handler from the event of the given type name.
pluginYou can write reusable lepont plugin by using the above APIs.
TBD
- [@lepont/async-storage][]
- Store data up to 6MB.
- [@lepont/share][]
- Share text and/or files from the browser.
- Wrapper of @react-native-community/cameraroll
- @lepont/device-info
- Access device information from browser
- based on react-native-device-info
- @lepont/communications`MIT
[@lepont/async-storage]: https://github.com/kt3k/lepont-async-storage
[@lepont/share]: https://github.com/kt3k/lepont-share
[AsyncStorage]: https://github.com/react-native-community/async-storage