Hi,
I have just followed above tutorial (with some modify) to subcribe to channel; send and revieve massage all in one page. It was ok at the very begining, but now I get these errors.
Hope somebody could help me with this. Thanks
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
import { openLightbox } from ‘wix-window’;
import { subscribe, unsubscribe } from ‘wix-realtime’;
import {publishMessage} from ‘backend/realtime’;
let channelP;
let userEmail;
let userF = wixUsers.currentUser; let ID ; let kenh;
let chatHis =;
let tempData=;
let preLoad = “abc”;
let ava; let roll;// let channels;
$w.onReady( async function () {
$w(“#videoPlayer”).seek(8);
chatHis = [
{
“_id”: “id0”,
“nameChat”: “…”,
“timeChat”: “…n”,
“textChat”: “…”,
“imageChat”: “https://pa1.narvii.com/6690/95ca11c193c1d912e26b493ff6154310656c0d11_hq.gif”
}
];
$w(“#htmlChat”).postMessage({mess0:chatHis})
if (wixUsers.currentUser.loggedIn) {
ID = wixUsers.currentUser.id;
$w(‘#sending’).text = ID;
await userF.getEmail()
.then( (email) => { userEmail = email});// console.log(userEmail);
wixData.query(“profile”)
.eq(‘email’,userEmail)
.find()
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0];
} else { }
} )
intializeMember(wixUsers.currentUser.id);
} else {
subscribeToVisitorChannel();
}
wixUsers.onLogin((user) => {
intializeMember(user.id);
unsubscribe({channel: {name: ‘visitors’}});
});
let list;
$w.onReady( () => {
$w(“#dataset2”).onReady( () => {
let itemObj = $w(“#dataset2”).getCurrentItem();
channelP = itemObj.channel;$w(‘#text113’).text = channelP;
list = channelP.split(“|”);
kenh = itemObj.allchannel.split(“|”);$w(“#subscriptions”).show();
ava = itemObj.pic; roll = itemObj.tutorRoll
} );
} );
let channels = await wixData.query(‘SubType’).find();//.eq(“channelResource”,channelP)
let options = channels.items.map( channel => ( { label: channel.type, value: channel._id } ));
$w(‘#subscriptions’).options = options;
$w(‘#subscriptions’).options = $w(‘#subscriptions’).options.filter((option) => option.label === list[0]||option.label === list[1]||option.label === list[2]||option.label === list[3]||option.label === list[4]||option.label === list[5]||option.label === list[6]||option.label === list[7]||option.label === list[8]||option.label === list[9]||option.label === list[10]);
$w(‘#sendButton’).onClick( () => {
let MESS = {
“teacher”:roll,
“gramVD”:“…”,
“gramColor”:“02330”,
“gramGT”:“…”,
“gramPic”:“https://mk0ediweeklycommok1n.kinstacdn.com/wp-content/uploads/2020/01/Engineered-Design-Insider-Mercedez-EV-conceptOil-Gas-Automotive-Aerospace-Industry-Magazine.jpg”,
“gramVid”:“Why You Spend So Much Money At Target - YouTube”,
“taskType”:“t w”,
“taskCode”:“py sx”,
“taskDe”:“…”,
“taskDA”:“…”,
“taskImage”:“https://ichef.bbci.co.uk/news/976/cpsprodpb/65DE/production/_117287062_wuling.jpg”,
“taskVid”:“How Social Security Works - YouTube”,
“text”: $w(‘#message’).value,
“color”:$w(‘#textBoxColor’).value//reciever
}
$w(‘#sending’).show();
$w(‘#sendButton’).disable();
let promises = $w(‘#subscriptions’).value.map( (subscription) => {
let selectedChannel = channels.items.find(channel => channel._id === subscription);
return publishMessage(
selectedChannel.channelName,
selectedChannel.channelResource,
//$w('#message').value,
MESS,
);
} );
Promise.all(promises)
.then( () => {
$w('#sendButton').enable();
$w('#sending').hide('fade');
} );
} );
$w(“#htmlTeach”).onMessage( (event) => { //ON MESS
let R = event.data.toLowerCase()
let m = R.split(“#”);
if (Array.from(m)[0] ===“s”){
$w(‘#text114’).text =m[1]
wixData.query(“c”)
.eq(“word”,m[1])
.find()
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0];
$w(‘#text115’).text = firstItem.pinyin;
$w(‘#text116’).text = firstItem.mean1
$w(‘#boxS’).expand();$w(‘#boxS’).scrollTo()
}})
}
if(Array.from(m)[0] ===“a”){ $w(‘#textBoxColor’).value = Array.from(m)[1] }
if(Array.from(m)[0] ===“x”){$w(‘#boxS’).collapse()}
})//END MESS
});
function subscribeToVisitorChannel() {
const visitorChannel = { name: ‘visitors’ };
return subscribe(visitorChannel, showBreakingNews);
}
function showBreakingNews({ payload }) {
// $w(‘#breakingText’).html = <h6><span style="color:${payload.color}">(${payload.time}) ${payload.message}</span><h6>
; // $w(‘#breakingStrip’).show(“fade”);
if (payload.message !== preLoad){
preLoad = payload.message
let x = payload.message.split("|");
if( x[0] === "@"){$w('#text113').text = x[1];
$w('#image').src =ava;
$w("#htmlTeach").postMessage({mess0:x[1],mess1:x[3],mess2:x[2]})
$w('#textBoxHis').value = x[0]+ "\n"+$w('#textBoxHis').value;
let i = chatHis.length;
let chatNow = [
{
"_id": "id"+i,
"nameChat": "man",
"timeChat": "time",
"textChat": x[1],
"imageChat": $w('#image').src
}
]
chatHis = chatNow.concat(chatHis);//console.log(tempData)
$w(“#htmlChat”).postMessage({mess0:chatHis})
}else{
$w(‘#textBoxHis’).value = x[0]+ “\n”+$w(‘#textBoxHis’).value;
let i = chatHis.length;
let chatNow = [
{
“_id”: “id”+i,
“nameChat”: “Nam”,
“timeChat”: “time”,
“textChat”: x[1],
“imageChat”: $w(‘#image’).src
}
]
chatHis = chatNow.concat(chatHis);//console.log(tempData)
$w(“#htmlChat”).postMessage({mess0:chatHis})
}
}
}//recieve payload end
async function intializeMember(userId){
let subscriptions = ;
//$w(‘#settings’).show();
const initialSubscriptions = await getSubscriptions(userId);
subscribeToMemberChannels(initialSubscriptions, subscriptions);
wixData.replaceReferences(‘Subscriptions’, ‘types’, userId, kenh);
// $w(‘#settings’).onClick(() => {
//openLightbox(‘Subscriptions’, subscriptions)
//.then(({ added, removed }) => {
//subscribeToMemberChannels(added, subscriptions);
//unsubscribeFromChannels(removed, subscriptions);
//});
//});
}
function getSubscriptions(userId) {
return wixData.queryReferenced(‘Subscriptions’, userId, ‘types’)
.then(({ result: { items } }) => items)
.catch(err => {
wixData.insert(‘Subscriptions’, { _id: userId });
return ;
});
}
function subscribeToMemberChannels(channels, subscriptions) {
channels.forEach(channel => {
let memberChannel = { name: channel.channelName, resourceId: channel.channelResource };
subscribe(memberChannel, showBreakingNews)
.then(subscriptionId => {
subscriptions.push(channel);
});
});
}
function unsubscribeFromChannels(removed, subscriptions) {
removed.forEach(id => {
let toRemove = subscriptions.find(subscription => subscription._id === id);
let toRemoveIndex = subscriptions.findIndex(subscription => subscription._id === id);
unsubscribe({ channel: { name: toRemove.channelName, resourceId: toRemove.channelResource } })
.then(() => {
subscriptions.splice(toRemoveIndex, 1);
});
});
}
export function tableGram_rowSelect(event) {
$w(“#htmlTeach”).postMessage({mess0:$w(‘#message’).value,mess1:$w(‘#textBoxColor’).value,mess2:$w(‘#textImageshow’).value,mess3:$w(‘#textImageTask4’).value,
mess4:$w(‘#textword’).value,mess5:$w(‘#textPY’).value,mess6:$w(‘#textMean’).value,mess7:$w(‘#textDetailCT’).value,
mess8:$w(‘#textType’).value,mess9:$w(‘#textCode’).value,mess10:$w(‘#textDebai’).value,mess11:$w(‘#textGoiy’).value,mess12:$w(‘#input1Dapan’).value,
mess13:$w(‘#textBoxVidshow’).value,mess14:$w(‘#textImageshow’).value
})
}
export function buttonCloseS_click(event) {
$w(‘#boxS’).collapse()
}