Add New Hybrid Page
The following steps will guide you on how to add a new hybrid page in your app.
Step 1: Create a new Vue file
Create a new Vue file inside the /pages/hybrid folder, for example, pages/hybrid/message.vue.
Step 2: Fill the content
In the message.vue file, you will add the following code:
<template>
<div>
<button @click="sendMessage">Send a message</button>
<div v-for="message in messages" :key="message">
{{ message }}
</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { definePageMeta } from "#imports";
import {
listenFromPlatform,
sendToPlatform,
} from "~/composables/hybrid/platformBridge";
import {
eventManager,
EVENT_ON_APP_BRIDGE_READY,
} from "~/composables/hybrid/eventManager";
import { isWebClient } from "~/config";
import { convertToJsonObject } from "~/helpers/jsonConvert";
definePageMeta({
layout: "hybrid",
});
const messages = ref<string[]>([]);
function sendMessage() {
sendToPlatform({
eventName: "onSendMessage",
payload: {
message: "Hello from the web client",
},
});
}
function listenReceiveMessage() {
listenFromPlatform({
eventName: "doReceiveMessage",
callback: (data) => {
const parsedData = !isWebClient ? convertToJsonObject(data) : data;
messages.value.push(parsedData?.message);
},
});
}
eventManager.on(EVENT_ON_APP_BRIDGE_READY, () => {
listenReceiveMessage();
});
</script>
Step 3: Access the page
You can access the page by http://localhost:3000/hybrid/message.
Code Breakdown
Template
The template section defines the HTML structure of the component. It consists of two main parts: a button and a div.
- Button
<button @click="sendMessage">Send a message</button>
The button element has a @click directive that is bound to the sendMessage function. When the button is clicked, the sendMessage function is triggered.
- Div
<div v-for="message in messages" :key="message">
{{ message }}
</div>
- The div element has a
v-fordirective that is used to loop over the messages in the messages array and display each message. - The
keyattribute is set tomessageto ensure that Vue can keep track of the elements in the loop and re-render efficiently when there’s a change.
Script
The script section contains the JavaScript code for the component. It consists of several imports and functions that are used to implement the functionality.
- Imports
import { ref } from "vue";
import { definePageMeta } from "#imports";
import {
listenFromPlatform,
sendToPlatform,
} from "~/composables/hybrid/platformBridge";
import {
eventManager,
EVENT_ON_APP_BRIDGE_READY,
} from "~/composables/hybrid/eventManager";
import { isWebClient } from "~/config";
import { convertToJsonObject } from "~/helpers/jsonConvert";
reffrom “vue”: This is a Vue helper function that is used to create reactive data.definePageMetafrom “#imports”: This function is used to define the metadata for the page, such as the layout to be used.listenFromPlatformandsendToPlatformfrom “~/composables/hybrid/platformBridge”: These functions are used to listen to and send events to the platform.eventManagerandEVENT_ON_APP_BRIDGE_READYfrom “~/composables/hybrid/eventManager”: eventManager is an instance of an event manager that is used to manage events in the hybrid environment, andEVENT_ON_APP_BRIDGE_READYis a constant that represents the event fired when the platform bridge is ready.isWebClientfrom “~/config”: This is a flag that indicates whether the current environment is a web client or not.convertToJsonObjectfrom “~/helpers/jsonConvert”: This function is used to convert a string to a JSON object.
- Reactive data
const messages = ref<string[]>([]);
messagesis an array of messages that is created using thereffunction. It’s a reactive data source, which means that its value can be updated and any component that uses it will be re-rendered automatically.
- sendMessage function
function sendMessage() {
sendToPlatform({
eventName: "onSendMessage",
payload: {
message: "Hello from the web client",
},
});
}
- The
sendMessagefunction is used to send a message to the platform. It uses thesendToPlatformfunction to send the message to the platform. TheeventNameis set toonSendMessageand thepayloadis set to an object containing the message.
- listenReceiveMessage function
function listenReceiveMessage() {
listenFromPlatform({
eventName: "doReceiveMessage",
callback: (data) => {
const parsedData = !isWebClient ? convertToJsonObject(data) : data;
messages.value.push(parsedData?.message);
},
});
}
- The
listenReceiveMessagefunction is used to listen to thedoReceiveMessageevent from the platform. It uses thelistenFromPlatformfunction to listen to the event. When the event is fired, the callback function is triggered. The callback function receives the data sent by the platform and adds it to the messages array.
- onAppBridgeReady event
eventManager.on(EVENT_ON_APP_BRIDGE_READY, () => {
listenReceiveMessage();
});
- The
onAppBridgeReadyevent is fired when the platform bridge is ready. TheeventManagerinstance is used to listen to the event. When the event is fired, the callback function is triggered. The callback function calls thelistenReceiveMessagefunction to listen to thedoReceiveMessageevent from the platform.