Building a realtime chat app with GraphQL Subscriptions
Learn how subscriptions in Hasura GraphQL Engine can be used to build a full-fledged real-time group chat app on Postgres with React and Apollo.
Pro Tip: There's no backend code involved. Everything is declarative.
TLDR
Hasura allows us to build a realtime GraphQL API without writing any backend code.
To save bandwith we use a subscription to listen for message events, then run a query to fetch only the new messages.
Every two seconds our frontend runs a user_online mutation to populate an online users list.
A user_typing mutation is run whenever we type few characters and a subscription is used to fetch the last typing user.
Introduction
Hasura allows us to instantly create a realtime GraphQL API from our data. In this tutorial we walk through creating a group chat application, without needing to write any backend code, using React and Apollo. The focus is on data models that we store in Postgres rather than full chat functionality.
Data Modelling
Users
When a user signs up we insert their chosen username and generate their ID. We also track when they last typed and were seen.
user (
id SERIAL PRIMARY KEY
username TEXT UNIQUE
last_seen timestamp with time zone
last_typed timestamp with time zone
)
Messages
For our tutorial we will just be inserting messages, not editing or deleting, but if we wanted to in the future Hasura will autogenerate the mutations for us. We could also extend this by adding features such as multiple different chatrooms.
message (
id SERIAL NOT NULL PRIMARY KEY
"text" TEXT NOT NULL
username INT FOREIGN KEY REFERENCES user(username) NOT NULL
"timestamp" timestamp with time zone DEFAULT now() NOT NULL
)
Online users
To query the users online we create a Postgres view that fetches all users with last_seen less than 10 seconds ago.
CREATE OR REPLACE VIEW public."user_online" AS
SELECT "user".id,
"user".username,
"user".last_typed,
"user".last_seen
FROM "user"
WHERE ("user".last_seen > (now() - '00:00:10'::interval));
Typing Indicator
To query the last person typing we create a similar view with last_typed within the past 2 seconds.
CREATE OR REPLACE VIEW public."user_typing" AS
SELECT "user".id,
"user".username,
"user".last_typed,
"user".last_seen
FROM "user"
WHERE ("user".last_typed > (now() - '00:00:02'::interval));
Frontend
Creating a user
At user signup we insert a single row into the users table
We take the returned id and username and store it in our app's state management.
User online events
Every two seconds we run a mutation to update our user's last_seen value. For example, with React using UseEffect or componentDidMount we could call the mutation inside a setInterval. Be mindful when using setInterval inside a React hook.
Subscribe to a query and render all the data that comes back.
Use the subscription as an event notification then run our own custom fetching logic.
Option two is best for our chatroom use case because we can do some work to just fetch new messages. Otherwise every new message we would also come with all previous ones.
On load we fetch all existing messages by setting $last_received_id to -1 and $last_received_ts to a timestamp well into the past such as '2018-08-21T19:58:46.987552+00:00'