web3mq

Creator: coderz1093

Last updated:

0 purchases

web3mq Image
web3mq Images

Languages

Categories

Add to Cart

Description:

web3mq

Quick Start #
REQUIREMENTS #

Dart SDK: “>=2.18.5 <4.0.0”
A Web3MQ API Key

Add dependency #
Add this to your package's pubspec.yaml file, use the latestversion
dependencies:
web3mq: 0.1.2
copied to clipboard
You should then run flutter packages get
Example Project #
There is a detailed Flutter example project in the example folder. You can directly run and play on it.
Setup API Client #
First you need to instantiate a chat client. The chat client will manage API call, event handling and manage the web socket connection to Web3mq servers. You should only create the client once and re-use it across your application.
final client = Web3MQClient("api-key");
copied to clipboard
Logging #
By default, the chat client will write all messages with level Warn or Error to stdout.
Change Logging Level #
During development, you might want to enable more logging information, you can change the default log level when constructing the client.
final client = Web3MQClient("api-key", logLevel: Level.INFO);
copied to clipboard
Custom Logger #
You can handle the log messages directly instead of have them written to stdout, this is very convenient if you use an error tracking tool or if you want to centralize your logs into one facility.
myLogHandlerFunction = (LogRecord record) {
// do something with the record (ie. send it to Sentry or Fabric)
}

final client = Web3MQClient("api-key", logHandlerFunction: myLogHandlerFunction);
copied to clipboard
Wallet Connector #
Some methods that SDK provides require wallet signature, you should setup the WalletConnector before calling those methods.
client.walletConnector = walletConnector;
copied to clipboard
abstract class WalletConnector {
/// Gets the signature of `personal_sign`.
Future<String> personalSign(String message, String address,
{String? password});

/// Connects wallet, and gets the user wallet info.
Future<Wallet> connectWallet();
}

abstract class Wallet {
/// account_id support CAIP-10
final List<String> accounts;

Wallet(this.accounts);
}
copied to clipboard
Offline storage #
To add data persistence you can extend the class PersistenceClient and pass an instance to the ChatClient. Web3MQPersistenceClient is a default implementation.
client.persistenceClient = Web3MQPersistenceClient();
copied to clipboard
Register #
For any first-time user of Web3MQ's network, you'll need to register on Web3MQ's network.
This method needs wallet signature, make sure you have setup WalletConnector already. RegisterResponse contains your PrivateKey and UserId.
// Keep your private key in a safe place!
final registerResponse = await client.register(did, password);
copied to clipboard
Retrieve Private Key #
Whenever you want, you can retrieve your own PrivateKey through this method.
// Keep your private key in a safe place!
final privateKeyHex = await client.retrievePrivateKey(did, password);
copied to clipboard
Connect #
Get a User #
Get a user with DID and password, also with an duration for expired.
final user = await client.user(did, password, expiredDuration);
copied to clipboard
Or if you has the PrivateKey .
final user = await client.user(did, privateKeyHex, expiredDuration)
copied to clipboard
you could persist user for the connection next time.
Connect with a User #
Now you can connect to Web3MQ with the User which you may want to persist.
await client.connectUser(user)
copied to clipboard
listen the wsConnectionStatusStream to track connection status:
client.wsConnectionStatusStream.listen((event) {
// handle the event
});
copied to clipboard
Notifications #
Subscribe #
Subscribe a topic with topicId, then you can receive notifications from that topic.
client.subscribeTopic(topicId);
copied to clipboard
Receive #
You can use the following method to subscribe notifications from the web3mq server.
client.notificationStream.listen( (notifications) {
// handle the notifications.
});
copied to clipboard
Read Status #
You may need other side to know if you have read the notification.
client.markNotificationsRead(notificationsIds);
copied to clipboard
Query #
You can query all historical notifications by types and pagination.
Page<Notification> res = await client.queryNotifications(type, pagination);
copied to clipboard
Chat #
Channel List #
To keep track of the list of channels, listen to the channelsStream event:
client.state.channelsStream.listen((event) {
// handle the channel list
});
copied to clipboard
Sending message #
To send a text message, call the sendText method with the message text and the ID of the topic:
client.sendText('hello, world!', topicId)
copied to clipboard
Message sending status #
To receive updates on the message sending status, listen to the messageUpdated event:
client.on(EventType.messageUpdated).listen((event) {
// handle the message status update
final status = event.messageStatusResponse;
}
copied to clipboard
Receiving new messages #
To receive new messages, listen to the newMessageStream event:
client.newMessageStream.listen((message) {
// handle the message.
}
copied to clipboard
Query the message list #
To query the message list, call the queryMessagesByTopic method with the ID of the topic and a pagination object:
client.queryMessagesByTopic('topicId', pagination)
copied to clipboard
Create Thread #
To create a thread, call the createThreadByMessage method with the ID of the original message, the ID of the topic, and the name of the thread:
client.createThreadByMessage(messageId, 'topicId', 'threadName')
copied to clipboard
Thread List #
To query the list of threads by given topic.
final list = await client.threadListByTopic('topicId')
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.