@textile/hub > Users > watchSentbox
Users.watchSentbox() method
watchSentbox watches the sentbox for new mailbox events. Returns a listener of watch connectivity states.
Signature:
watchSentbox(id: string, callback: (reply?: MailboxEvent, err?: Error) => void): grpc.Request;
Parameters
Parameter | Type | Description |
---|---|---|
id | string | the mailbox id |
callback | (reply?: MailboxEvent, err?: Error) => void | handles each new mailbox event. |
Returns:
grpc.Request
listener. listener.close will stop watching.
Example
The local user's own sentbox can be decrypted with their private key
import { Users, MailboxEvent, PrivateKey } from '@textile/hub'
const userID = PrivateKey.fromRandom()
const callback = async (reply?: MailboxEvent, err?: Error) => {
if (!reply || !reply.message) return console.log('no message')
const bodyBytes = await userID.decrypt(reply.message.body)
const decoder = new TextDecoder()
const body = decoder.decode(bodyBytes)
console.log(body)
}
// Requires userID already be authenticated to the Users API
async function startListener(users: Users) {
const mailboxID = await users.getMailboxID()
const closer = await users.watchInbox(mailboxID, callback)
}