← Back to SDB Docs

Chat

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.

The Class Room

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.

Reading Messages

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"
  }
]

Sending a Message

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.

Good to Know

  • The room keeps the latest 500 messages. Older ones are cleaned away automatically.
  • You can send up to 10 messages per second. Go faster and you get a 429 status back. A person typing will never hit this, but a loop by accident might.
  • Messages can't be edited or deleted. Once it's sent, it's in the room, so be kind.
  • Only username and content are part of a message. Other apps in the room only know about these two, so stick to them.

What Your Chat Needs

Take these one at a time. After each step you have something that works.

  1. 1
    Show the messages. GET the room, go through the array with a loop and ez.dom.create an element for each one, showing who wrote it and what they said.
  2. 2
    Keep it fresh. New messages don't arrive by themselves, you have to ask again. Use 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.
  3. 3
    Send. Two inputs (your name and your message) and a button. On click, POST them and empty the message input.
  4. 4
    Show who's been chatting. Make a list of the usernames in the messages, each name only once.
async function loadMessages() {
  // GET the room and draw the messages here
}

loadMessages();                  // once right away
setInterval(loadMessages, 2000); // and again every 2 seconds

Ideas for Extras

Got the basics working? Pick whatever sounds fun:

  • Enter to send — listen for keydown on the message input and send when the key is Enter.
  • Scroll to the bottom — the newest message is last, so after drawing the list, scroll it down.
  • A color per user — turn each username into a color so it's easy to see who is talking.
  • Remember the name — save the username with ez.store.local so you don't have to type it every time.
  • Highlight your own messages — style messages with your username differently.
  • Don't send empty messages — check the input before you POST.

Your Own Room (optional)

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.