Making chat with emojis in Gameface
5/30/2024
Kaloyan Geshev
Creating a chat in Gameface is a straightforward process, thanks to the built-in support for websockets. This is especially true when utilizing a library like socket.io . We have a topic in our documentation that explains how the official socket.io chat sample works seamlessly in Gameface.
Showcase overview
In this showcase, we have enhanced the socket.io chat sample by adding several features:
- A scrollable container for messages, using the Gameface scrollable-container component , as Gameface does not natively support a scroll slider.
- Connect and disconnect user messages displayed in the chat.
- Timestamps for all messages.
- Displaying the user ID for incoming messages.
- A panel for sending emojis in the chat.
This sample focuses on how to send and show emojis in the chat, as Gameface does not currently support OS emojis natively. And it has the added bonus of allowing you to use your own custom emojis. One straightforward method is to use SVG emojis. For this sample, we used a small set of SVG emojis from OpenMoji to demonstrate the simplest way of adding emoji support to a chat in Gameface.
Source location
You can review the complete sample source within the ${Gameface package}/Samples/uiresources/UITutorials/ChatEmojis
directory.
To run the sample, follow these steps:
- Do
npm i
in this directory. - Run the chat server by executing
node server.js
in this directory. - Open two or more instances of Gameface and chat between them.
Sample specifics
Chat messages container and buttons
We followed the socket.io sample to create the messages container and the send button. We then enhanced it by adding scroll functionality to the messages container using the gameface-scrollable-container
and included a button for toggling the emoji panel.
Sending messages
To send the text typed in the chat input to the server, an event is emitted when the Send
button is clicked or when the Enter
key is pressed while the input is focused.
After the server processes the message, it emits an event to all connected users, updating their chat UI. Check the next section for details.
Receiving messages
Receiving messages from the server on the front end involves subscribing to a socket
event:
This code creates a new list
element to hold the message from the server, including the user ID if the message is from another user.
The element is appended to the messages container, and if the message is from the current user, the scrollable container scrolls to the bottom.
Know issue:
There is a need to wait for 7 frames to ensure the scrollable container correctly scrolls to the last message. The scrollable container automatically resizes when a new list
element is appended, which takes about 6 frames.
Without waiting, the container might not scroll properly.
The waitForFrames
function is defined as follows:
Server
On the server side, messages sent from the front end are processed by listening for socket
events.
A timestamp is generated for each message. The server then emits an event to the front end with the processed message in HTML format, along with the sender’s ID.
Chat emojis - Model
To simplify the instantiation of all emojis in a panel, we defined an array as a Gameface data binding model. Leveraging Gameface’s data binding , we can easily iterate over this model and display the emojis in the HTML page.
The model is structured as follows:
Chat emojis (FE) - Emojis panel
We added a button to the right of the chat input that opens a panel with all supported chat emojis, allowing users to add emojis to their messages before sending them. Selecting an emoji from the panel inserts its code into the message input, surrounded by colons. For example, :1:
.
The panel was created using the gameface-tooltip component to wrap the emojis in a panel, and the gameface-scrollable-container component to enable scrolling if the emojis overflow the panel.
Here, we use data binding to render the emoji SVGs in the panel with data-bind-style-background-image-url="{{iter}}"
, which sets the background of the div to the emoji URLs from the model.
Note: The tooltip might not be positioned correctly as its target is at the bottom right of the page. To fix this, override the tooltip’s transform
property:
Chat emojis (BE) - Resolving emojis
When a message is received on the server, any emoji codes in the message should be resolved. To achieve this, extend the source in the Server section.
Here, newMessage
contains the processed message with all emoji codes replaced by img
elements with the corresponding emoji SVG URLs from the emojis map.
Chat user connected and disconnected
Additionally, we added messages indicating when a user connects or disconnects from the chat.
This is easily handled on the server side:
On the front end, we handle the emitted events by subscribing to the user connected
and user disconnected
events and reading the user ID:
For each connection or disconnection, we append a list
item to the messages container.
Styles
To check the chat styles, refer to the ${Gameface package}/Samples/uiresources/UITutorials/ChatEmojis/styles.css
file.