Progressive Web Apps: New FE systems
Sometimes Connectivity Becomes a Bottleneck.
In enterprise environments, we often take stable internet connectivity as something given. However, real world conditions frequently challenge this assumption, potentially disrupting critical business operations. This article details how we transformed a traditional online only ERP system into a more reliable system with resilient offline capable solution. By leveraging browser-based storage solutions like IndexedDB, employing synchronization mechanisms, and using Progressive Web Apps (PWA).
Initially, the system followed a traditional client server architecture where all business logic resided on the backend. While this architecture works well in environments with reliable connectivity, it presented several challenges:
So defining this, we had to improvise and see how we can make things better and also do without connectivity since it isnt available initially, we implemented an offline system with some internet required using Progressive Web Apps (PWA), effectively moving critical business logic to the frontend while maintaining data integrity and synchronization with the core ERP system.
Some Core Components:
// Initialize IndexedDB using Dexie.js
import Dexie from 'dexie';
interface LocalUsers{
id:string;
name:string;
role:string;
dob:string;
phone_no:string
}
interface LocalTrx {
id: string;
syncId:string;
created_at:string;
amount:string;
isSynced:boolean;
modified:string;
}
export class ArticleDatabase extends Dexie {
transaction!: Table<LocalTrx>;
users!: Table<LocalUsers>;
constructor(){
super("articleDB")
}
this.version(1).stores({
// define the fields you'll like to query by or find items by within the indexDB
transactions: 'id, date, amount, isSynced',
users: 'id, name, role'
});
}
//create the db
export const db=new ArticleDatabase()
// Open the database
db.open().then(() => {
console.log("Database opened successfully!");
})
.catch((error) => {
console.error("Failed to open database:", error);
});
// Adding a new transaction to IndexedDB
import db from ../db
async function addTransaction(transaction) {
try {
const trx = await db.transactions.add(transaction)
console.log("Trx added", trx)
} catch (err) {
console.log("Failed creating trx", err)
}
}
//service workers can be setup easily, recently by default nextJS apps do come with service workes with vite, you can use the vite-pwa plugin
System Architecture Flow
The architecture was divided into three main phases: initialization, transaction processing, and synchronization. The flowchart below shows how data flows between these stages.
Initialization Phase
When the system starts, it checks the network connection:
Transaction Processing When users perform a new transaction:
Synchronization Phase When connectivity is restored:
Since we manage everything on the frontend how reliant is our service against securing customers Information.
领英推荐
Authentication & Authorization
In any enterprise system, securing sensitive user information is critical. Our solution ensures that:
To mitigate the risks of using locally stored tokens, we:
Data Integrity & Conflict Resolution
Syncing data between the client and server introduces potential issues with data integrity, especially if multiple devices or users are making changes to the same data offline. To address this:
//we try to make sure the offline is considered first, because it's the important bit of the system.
async function resolveConflict(localTransaction, serverTransaction) {
// Compare timestamps determine which transaction should prevail
if (new Date(localTransaction.timestamp) > new Date(serverTransaction.timestamp)) {
// Local transaction wins
await syncTransactionWithServer(localTransaction);
} else {
// Server transaction wins
await updateLocalTransaction(serverTransaction);
}
}
async function updateLocalTransaction(transaction) {
// Update the local transaction in IndexedDB to reflect the server's state
await db.transactions.put(transaction);
}
Network Security
Given that data is transmitted over the network once connectivity is restored, we ensured:
Alternatives to PWA & IndexedDB
While IndexedDB is a solid choice for client-side data storage in PWAs, there are other options available depending on the application's complexity and requirements:
Looking Ahead: Future Trends in PWA
As web technologies continue to evolve, so do the possibilities for applications like this. Emerging trends include:
I myself can’t wait to explore how these technologies will transform the landscape of offline and distributed applications. With the rapid advancements in powerful machines and laptops, we have the opportunity to harness this increased computing power to deliver even more sophisticated and efficient software for users. At the same time, we must not forget the importance of catering to mobile devices and less capable devices, ensuring that our solutions are accessible and optimized across all platforms. The potential is enormous, and I’m excited to continue pushing the boundaries of what's possible with PWAs.
Note: What's Next
We’ll be taking things up. Using Djuix.io as our backend and React / Angular for our frontend, we would implement a proper basic flow. Stay tuned for more updates as we continue to enhance our approach to building amazing apps.
Anyway, I hope you enjoyed this and learned something new. I certainly did. I'd also love to hear your thoughts and experiences.
Until then.
Python | Data Analyst | Climate Scientist | Environmental Analyst
3 个月Nice job. Would be nice to see this system been adopted by all payments platforms, as it can effortlessly improve customers experience through seamlessly transactions