VK Bridge Core
Usage
import bridge from '@vkontakte/vk-bridge';
bridge.send('VKWebAppInit');
bridge.subscribe((e) => console.log(e));
For use in a browser, include the file dist/browser.min.js
and use as follows
<script src="https://unpkg.com/@vkontakte/vk-bridge/dist/browser.min.js"></script>
<script>
vkBridge.send('VKWebAppInit');
</script>
API Reference
bridge.send(method[, params])
Sends a message to native client and returns the Promise
object with response data
Parameters
method
required The VK Bridge methodparams
optional Message data object
Example
bridge
.send('VKWebAppGetEmail')
.then((data) => {
console.log(data.email);
})
.catch((error) => {
});
You can also use imperative way
try {
const data = await bridge.send('VKWebAppGetEmail');
console.log(data.email);
} catch (error) {
}
bridge.subscribe(fn)
Subscribes a function to events listening
Parameters
fn
required Function to be subscribed to events
Example
bridge.subscribe((event) => {
if (!event.detail) {
return;
}
const { type, data } = event.detail;
if (type === 'VKWebAppOpenCodeReaderResult') {
console.log(data.code_data);
}
if (type === 'VKWebAppOpenCodeReaderFailed') {
console.log(data.error_type, data.error_data);
}
});
bridge.send('VKWebAppOpenCodeReader', {});
bridge.unsubscribe(fn)
Unsubscribes a function from events listening
Parameters
fn
required Event subscribed function
Example
const fn = (event) => {
};
bridge.subscribe(fn);
bridge.unsubscribe(fn);
bridge.supports(method)
Checks if an event is available on the current device
Parameters
method
required The VK Bridge method
bridge.isWebView()
Returns true
if VK Bridge is running in mobile app, or false
if not
bridge.isIframe()
Returns true
if VK Bridge is running in iframe, or false
if not
bridge.isEmbedded()
Returns true
if VK Bridge is running in embedded app, or false
if not
bridge.isStandalone()
Returns true
if VK Bridge is running in standalone app, or false
if not
Middleware API
Middlewares are pieces of code that intercept and process data between sending and receiving. Thus, by creating middlewares, you can easily log data, modify data before sending it, talking to an asynchronous API, etc. If you've used Redux, you were also probably already familiar with the concept—a similar is used here.
applyMiddleware(middleware1, ..., middlewareN)
Creates the VK Bridge enhancer that applies middleware to the send
method. This is handy for a variety of task such as logging every sent
event. Returns the VK Bridge enhancer applying the middleware.
Parameters
middlewareN
A middleware to be applied
Example
import bridge, { applyMiddleware } from '@vkontakte/vk-bridge';
const logger =
({ send, subscribe }) =>
(next) =>
async (method, props) => {
const result = await next(method, props);
console.log(result);
return result;
};
const enhancedBridge = applyMiddleware(logger)(bridge);
Functions
parseURLSearchParamsForGetLaunchParams(urlSearchParams: string)
Parse URL search params for get provided to mini app launch params.
Parameters
urlSearchParams
a.k.a window.location.search
.
Example
import { parseURLSearchParamsForGetLaunchParams } from '@vkontakte/vk-bridge';
const { vk_platform, vk_viewer_group_role, vk_user_id } = parseURLSearchParamsForGetLaunchParams(
window.location.search,
);
console.log(vk_platform);
console.log(vk_is_app_user);
console.log(vk_user_id);