use chatbox in wix corvid code example

Example: use chatbox in wix corvid

import wixChat from 'wix-chat-backend';
import wixData from 'wix-data';

let options = {
"suppressAuth": true,
"suppressHooks": false
};

export function saveChannel(participantId, channelId) {   // see events.js
let toSave = {
"_id": participantId,
"channelId": channelId
};
wixData.save("ChatChannels", toSave, options)
}

export function sendChatMessage(messageText, userId) {
return findChatChannel(userId)
.then((channelId) => {
console.log("sending message through " + channelId)
wixChat.sendMessage({
"messageText": messageText,
"channelId": channelId,
"sendAsVisitor": false,
"metadata": {"type:": "notification"}
})
})
.then(() => {
console.log("Chat message sent");
})
.catch((error) => {
console.log(error);
});
}

function findChatChannel(userId) {
return wixData.query("ChatChannels")
.eq("_id", userId)
.find()
.then((results) => {
if (results.items.length > 0) {
let firstItem = results.items[0];
let channelId = firstItem.channelId
return channelId
} else {
let em = "no channel found for user"
throw em
}
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg)
});
}

Tags:

Misc Example