Event Handling
The Sociogram Mini Apps SDK allows your mini-app to listen for specific events sent back from the Sociogram host platform.
How Event Handling Works
- Mini-App sends a request: Your mini-app calls a WebApp method (e.g., WebApp.openInvoice)
- Host processes request: The Sociogram platform receives this message and performs the action
- Host sends a response: For actions that require feedback, the host platform sends a message back
- SDK receives and dispatches: The SDK's internal event listener captures this message and dispatches it
Direct Event Subscription
tsx
import WebApp from '@sociogram-dev/mini-apps-sdk';
const WebView = window.Sociogram.WebView;
const handleInvoiceClosed = (eventType, eventData) => {
console.log(`Received event: ${eventType}`, eventData);
const { invoiceId, status } = eventData;
if (status === 'success') {
console.log(`Invoice ${invoiceId} paid successfully!`);
} else {
console.log(`Invoice ${invoiceId} payment failed or was cancelled.`);
}
};
// Subscribe to events
WebView.onEvent('mini_app_invoice_closed', handleInvoiceClosed);
// To unsubscribe from an event:
// WebView.offEvent('mini_app_invoice_closed', handleInvoiceClosed);Advice
For most use cases, relying on the callbacks provided by methods like WebApp.openInvoice or WebApp.getFollowers is sufficient and recommended.