Updated 21 January 2025
In this blog, we will learn how to build a mobile app using Hyperview.
Hyperview is a React Native client for building server-driven mobile apps. It uses a simple XML-like format called HXML to define app screens, allowing the server to control the app’s user interface.
HXML is an XML-based format that defines native mobile UIs. It supports common UI elements, styling, and a behavior syntax for user interactions without requiring scripting.
This is a cross-platform library in React Native for rendering HXML in mobile apps. It can be used in both new and existing projects to deliver server-driven UIs efficiently.
Explore more about Hyperview on its official documentation.
Now we will implement the app, step by step.
To set up the environment, check out our previous blog, Getting Started With React Native, where we walk through the entire setup process.
1 |
npm i hyperview |
For a detailed understanding of the installation process and to install dependencies for Hyperview, refer to the Instawork Hyperview repository.
We’ll start by creating a server.js file.
This file creates a server that listens for requests on port 3000. It shows the local address where the server can be accessed on your network.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const express = require('express'); const { getLocalIPAddress } = require('./ipHelper'); const mainRoute = require('./mainRoute'); const app = express(); const port = 3000; // Use the main route app.use('/', mainRoute); // Start the server const ip = '0.0.0.0'; app.listen(port, ip, () => { console.log(`Hyperview server running at http://${getLocalIPAddress()}:${port}`); }); |
This code finds your computer’s local IP address to help connect with it on a network. If it can’t find one, it defaults to “localhost”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const os = require('os'); // Function to get the local IP address function getLocalIPAddress() { const interfaces = os.networkInterfaces(); for (const iface in interfaces) { for (const alias of interfaces[iface]) { if (alias.family === 'IPv4' && !alias.internal) { return alias.address; // Return the local IP } } } return 'localhost'; // Fallback to localhost } module.exports = { getLocalIPAddress }; |
Loads data from the API and creates the XML response using generateXml.js
. It sends this response when the app accesses the main route (/
).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
const express = require('express'); const { fetchData } = require('../helpers/apiService'); const { generateXml } = require('./generateXml'); const router = express.Router(); router.get('/', async (req, res) => { try { const apiData = await fetchData(); const xmlResponse = generateXml(apiData); res.set('Content-Type', 'application/xml'); res.send(xmlResponse); } catch (error) { res.status(500).send('Error generating XML.'); } }); module.exports = router; |
Generates the XML structure for the app, combining API data and styles. It ensures the XML content is dynamic and ready for the Hyperview client.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
const { getStyles } = require('./styles'); function generateXml(apiData) { const items = apiData.map((item, index) => { return ` <item key="${index + 1}" style="Item"> <text style="Item__Number">${index + 1}.</text> <text style="Item__Label">${item.title}</text> </item> `; }).join(''); const styles = getStyles(); return ` <doc xmlns="https://hyperview.org/hyperview"> <screen> ${styles} <body style="Main"> <text style="Header">Hyperview List</text> <list trigger="refresh" href="/case_studies/pull_to_refresh/refresh.xml" action="replace" > ${items} </list> </body> </screen> </doc> `; } module.exports = { generateXml }; |
Defines the design settings (like colors, padding, and fonts) for the app’s UI in one place. These styles are reused in the XML to keep the design consistent.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function getStyles() { return ` <styles> <style id="Main" flex="1" paddingTop="48" paddingLeft="24" paddingRight="24" backgroundColor="white" /> <style id="Header" fontSize="20" fontWeight="bold" marginBottom="16" textAlign="center" color="black" /> <style id="Item" flexDirection="row" alignItems="center" justifyContent="flex-start" height="48" borderBottomWidth="1" borderBottomColor="#eee" paddingLeft="8" /> <style id="Item__Number" fontSize="16" fontWeight="bold" marginRight="8" color="black" /> <style id="Item__Label" fontSize="16" color="black" /> </styles> `; } module.exports = { getStyles }; |
This code fetches data from an external website and returns it. If something goes wrong, it shows an error message and returns an empty list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const axios = require('axios'); // Function to fetch data from the external API async function fetchData() { try { const response = await axios.get('https://jsonplaceholder.typicode.com/posts'); return response.data; // Return API data } catch (error) { console.error('Error fetching data:', error.message); return []; // Return an empty array in case of an error } } module.exports = { fetchData }; |
This code build a mobile app using Hyperview that connects to a server at a specific address. It also includes a function to format dates, though it’s not yet implemented.
1 2 3 4 5 6 7 8 9 10 11 12 |
import Hyperview from 'hyperview/src/core/components/hv-root'; import React from 'react'; export default function App() { return ( <Hyperview entrypointUrl={'http://your device ip address:3000'} fetch={fetch} formatDate={function (date: Date | null | undefined, format: string | undefined): string | undefined { throw new Error('Function not implemented.'); } } /> ); } |
To run your app, follow these steps:
Open the terminal where your server.js file is located and run the following command
1 |
node server.js |
This will start your server.
Open your project in VSCode and open the terminal. Then, run the appropriate command
For Android
1 |
npx react-native run-android |
For IOS
1 |
npx react-native run-ios |
Use the Hyperview framework to build apps where the design and content are sent from the server, making it easy to update the app without changing the code.
Hyperview revolutionizes mobile app development by enabling server-driven UIs. Its integration with React Native ensures flexibility, dynamic content delivery, and simplified updates.
Thanks for reading this blog.
Please check my other blogs here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.