Get up and running with the Inspatial Cloud Client SDK in minutes. This guide shows you how to initialize clients, fetch data, and listen to real-time updates.
Create an instance of InCloudClient to interact with your backend APIs:
import { InCloudClient } from '@inspatial/cloud-client';// Initialize with your API endpointconst client = new InCloudClient('/api');// Or with a full URL for remote serversconst remoteClient = new InCloudClient('https://your-domain.com/api');
Listen to live changes using WebSocket connections:
1
Initialize and Connect
import { InLiveClient } from '@inspatial/cloud-client';const liveClient = new InLiveClient('wss://your-domain.com');// Start the connectionliveClient.start();
// Subscribe to a specific entryliveClient.onEntry('Product', '123', { name: 'product-listener', callback: (event, data) => { console.log(`Product ${event}:`, data); // Events: 'updated', 'deleted', 'field_changed' }});
4
Listen to Entry Type Events
// Subscribe to all productsliveClient.onEntryType('Product', { name: 'all-products', callback: (event, data) => { if (event === 'created') { console.log('New product created:', data); } }});
5
Clean Up
// Remove specific listenerliveClient.removeEntryListener('Product', '123', 'product-listener');// Leave all listeners for an entryliveClient.leaveEntry('Product', '123');// Disconnect when doneliveClient.stop();
import { MimeTypes } from '@inspatial/cloud-client';// Get MIME type from file nameconst mimeType = MimeTypes.getMimeTypeByFileName('document.pdf');// 'application/pdf'// Get by extensionconst type = MimeTypes.getMimeTypeByExtension('png');// 'image/png'// Get categoryconst category = MimeTypes.getCategory('mp4');// 'video'// Get all extensions in a categoryconst imageExts = MimeTypes.getExtensionsByCategory('image');
Here’s a complete working example combining REST and real-time clients:
import { InCloudClient, InLiveClient } from '@inspatial/cloud-client';// Initialize clientsconst client = new InCloudClient('/api');const liveClient = new InLiveClient('ws://localhost:8080');// Start live connectionliveClient.start();// Fetch initial dataconst products = await client.entry.getEntryList('Product', { limit: 20, filter: { status: 'active' }});console.log(`Loaded ${products.list.length} products`);// Listen for new productsliveClient.onEntryType('Product', { name: 'product-updates', callback: (event, data) => { if (event === 'created') { console.log('New product added:', data); // Update your UI with the new product } }});// Create a new productconst newProduct = await client.entry.createEntry('Product', { name: 'Wireless Mouse', price: 29.99, status: 'active'});// The live listener above will be triggered automatically
The real-time client automatically manages room subscriptions. When you add listeners, it joins the appropriate rooms, and when all listeners are removed, it leaves the room to optimize bandwidth.