Skip to content

Full Example: Realtime Chat ​

The Realtime Chat page is a group chat with private messages, built on Skapi's realtime connection. It needs a logged-in user. Open it in another browser or device with a second account and join the same group to chat.

Enter your project ID to open the demo.

You need your project ID to open the demo.

The Realtime Chat page of the template: an explanation of the realtime API, a form to create a group and the table of chat groups

realtime.html before a group is joined: create a group, or pick one from the table.

The chat in a group: the messages box with join notices and messages from two users, the participant list beside it, and the message form below

In a group. Every message, the sender's included, arrives through the realtime callback and is appended to the box; the list on the right shows who is in the group.

The dialog for a private message to one participant, with the message typed in

Clicking a name in the participant list opens the private message dialog. The message goes to that user only.

The dialog the other user sees: Private message from the sender, and the text

What the other user sees: a private message opens this dialog.

What it demonstrates ​

FeatureHow the page does it
ConnectingconnectRealtime() opens the WebSocket connection once the user is known, with one callback that receives every message.
Listing and creating groupsgetRealtimeGroups() lists the groups that have someone in them. Creating a group is joining a name nobody has joined yet.
Joining a groupjoinRealtime() with the group name from the URL hash; getRealtimeUsers() lists who is already there, and their profiles come from getUsers().
Group messagespostRealtime() with the form and the group name. Everyone in the group, including the sender, receives it as a message.
Private messagesClicking a name in the participant list opens a dialog; postRealtime() with a user ID instead of a group name delivers the message to that user only, as a private message.
Joining and leavingThe notice messages with the codes USER_JOINED, USER_LEFT and USER_DISCONNECTED keep the participant list current.

Download ​

The page is part of the full template: skapi-templates.zip. See Full Examples for how to run it.