Build your own chat app. Everyone in every class talks in the same room, so as soon as your page can read and send messages, you're chatting with real people.
There is one shared room for everyone. Use this exact URL for both reading and sending:
https://tinkr.tech/sdb/poly/chat
The backend is already running. Your job is only the page: show the messages and let people write new ones. You can do all of it with the ez library you already know.
Send a GET request to the room URL. You get back an array of messages, oldest first. Every message has an id, a
username
and the content.
const result = await ez.request.get('https://tinkr.tech/sdb/poly/chat');
const messages = result.body;
console.log(messages);
An example response:
[
{
"id": "0b7c1e7a-4f1d-4a8e-9a43-2d1f6c0e5b11",
"username": "Mari",
"content": "Hi everyone!"
},
{
"id": "5d2e9f30-8c6b-4b1e-b2a7-91c4e8d3f702",
"username": "Karl",
"content": "Hello from the other class"
}
]
Send a POST request to the same URL with a dictionary that has two strings:
username
and content.
Both are required. If one of them is missing, or isn't a string,
the server says no with a 400
status and the message is not saved.
const result = await ez.request.post('https://tinkr.tech/sdb/poly/chat', {
username: 'Mari',
content: 'Hi everyone!'
});
if (!result.ok) {
console.log(result.error);
}
The answer is the saved message, with its new id.
It will show up for everyone the next time they GET the room.
429
status back. A person typing will never hit this, but a loop by accident might.
username
and content
are part of a message. Other apps in the room only know about these two,
so stick to them.
Take these one at a time. After each step you have something that works.
ez.dom.create
an element for each one, showing who wrote it and what they said.
setInterval
to reload every couple of seconds. Clear the old list with
ez.dom.removeChildren
before drawing it again, or you'll see every message twice.
async function loadMessages() {
// GET the room and draw the messages here
}
loadMessages(); // once right away
setInterval(loadMessages, 2000); // and again every 2 seconds
Got the basics working? Pick whatever sounds fun:
keydown
on the message input and send when the key is Enter.
ez.store.local
so you don't have to type it every time.
Want a private room for testing, or just for your friends? Make your own database in your databases and use its URL instead of the class room. Everything on this page works the same way.