Hello everyone !!!
I think this post will help some who are creating social-networking sites…
So, here’s what I am going to show you…
There are 2 users - User A and User B
User A is in State A and User B is in State B
So, when User A types something in an input, a text (inside a box) will show in the page of User B at the same time
The code will give you more info →
- You should create a .jsw file and rename it as realtime.jsw
//realtime.jsw
import { publish } from 'wix-realtime-backend';
export function publishMessage(message) {
const channel2 = { name: 'messaging' };
return publish(channel2, { message: message }, { includePublisher: false });
}
Here’s the page code →
import { publishMessage } from 'backend/realtime';
import * as realtime from 'wix-realtime';
$w.onReady(function () {
const channel2 = { name: 'messaging' };
realtime.subscribe(channel2, type);
$w('#input').onInput((event) => {
return publishMessage(wixUsers.currentUser.id);
});
async function type({ payload }) {
if ($w('#notificationBox2').hidden) {
$w('#notificationText2').text = "user is typing";
$w('#linkButton2').link = payload.link;
$w('#notificationBox2').show('fade');
setTimeout(() => $w('#notificationBox2').hide('fade'), 3000);
} else {
setTimeout(() => type({ payload }), 3000);
}
}
});
Anyone can make necessary changes !!!