edmax_realtime_mongo_lite 0.0.4
edmax_realtime_mongo_lite: ^0.0.4 copied to clipboard
a lightweight Dart package designed to facilitate real-time data synchronization with a MongoDB-like structure over WebSocket. This package allows developers to easily listen to changes in a database [...]
Edmax Realtime Mongo Lite #
Edmax Realtime Mongo Lite is a lightweight Dart package designed to facilitate real-time data synchronization.
Supported Platform #
- Android
- iOS
- Web
- MacOS
- Windows
What Does the Package Do? #
- Connects to a WebSocket server to receive real-time updates.
- Listens for changes in the database and triggers corresponding actions in the application.
- Parses incoming JSON messages into structured Dart objects (
DataSnapshot). - Provides a simple API for developers to interact with real-time data, ensuring smooth and efficient updates in their Flutter or Dart applications.
Key Features #
- Singleton Pattern: Ensures only one instance of the WebSocket connection is active.
- Listener Management: Allows developers to register listeners to handle different events (e.g., data changes, disconnections, errors).
- JSON Parsing: Automatically parses JSON data into structured Dart objects for easier manipulation and use within the application.
- Error Handling: Includes built-in error handling to manage connection issues and unexpected data formats.
Why Should Developers Use It? #
- Ease of Use: Provides a straightforward API for setting up real-time data synchronization with minimal boilerplate code.
- Real-time Data: Perfect for applications that require instant updates from the server, such as chat apps, live feeds, or collaborative tools.
- Customizable: The package is designed to be flexible, allowing developers to customize their listeners and handle different types of data operations.
Example Usage #
//[Your App Entry Point main.dart]
//set your websocket url
void main() {
EdmaxRealtimeMongo(wsUrl: "ws://localhost:8080/");
runApp(const MAIN());
}
//[Your Class]
import 'package:edmax_realtime_mongo_lite/edmax_realtime_mongo_lite.dart';
import 'package:edmax_realtime_mongo_lite/tools/snapshot.dart';
import 'package:flutter/material.dart';
class HelloWorld extends StatefulWidget {
const HelloWorld({super.key});
@override
State<HelloWorld> createState() => _HelloWorldState();
}
class _HelloWorldState extends State<HelloWorld> with EdmaxRealtimeMongoListener {
@override
void initState() {
super.initState();
//Additionally pass url and snapshot
//[EdmaxRealtimeMongo.initialize(listener: this,endpoint: "http://xxxxx/load_users",snapshot: "users");]
//or pass them as empty strings if not needed if needed more info check ChangeLog 0.0.3
EdmaxRealtimeMongo.initialize(listener: this,endpoint: "",snapshot: "");
}
@override
void dispose() {
super.dispose();
//ALWAYS null and empty for both endpoint and snapshot
EdmaxRealtimeMongo.initialize(listener: null,endpoint: "",snapshot: "");
}
@override
Widget build(BuildContext context) {
return const Placeholder();
}
@override
void onDataChange(DataSnapshot dataSnapshot) {
print('Data changed: ${dataSnapshot.snapshot}');
}
@override
void onDataRemoved() {
print('Data removed Reload');
}
@override
void onDisconnected() {
print('Disconnected from the server');
}
@override
void onError(String error) {
print('Error: $error');
}
}
JSON Structure Requirements #
For this package to work correctly, your server must return JSON in the following structure:
{
"snapshot": "contents",
"operation": "update",
"data": {
"_id": "66b8b265d47bdeb08e7a8581",
"leading": "<img src=\"https://example.com/image.jpg\"/>",
"content": "Your content here",
"time": 1234567890,
"tag": "_community"
}
}
JSON Keys #
snapshot: A string that identifies the type of data snapshot.operation: A string that describes the operation performed (e.g.,update,delete).data: A map containing the actual data. It should include the following keys:_id: A string representing the unique identifier of the data.leading: A string that may contain HTML or other leading content.content: A string containing the main content.time: An integer representing a timestamp or time-related data.tag: A string that tags or categorizes the data.
Support #
If you encounter any issues or have suggestions for improvement, feel free to contact the package author at innocentmax6367@gmail.com.