GithubHelp home page GithubHelp logo

streaming-subscriptions-chat's Introduction

Realtime Chat using Streaming Subscriptions

This is the source code for a fully working group chat app that uses streaming subscriptions in Hasura GraphQL Engine. It is built using React and Apollo.

Run this example with Docker: docker compose up -d --build

Adapted from the original blogpost by Rishichandra Wawhal.

Deploy to Hasura Cloud

TLDR

  • Hasura allows us to build a real-time GraphQL API without writing any backend code.

  • Using streaming subscriptions we fetch the last ten messages then stream 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 a few characters and a subscription is used to fetch the last typing user.

Introduction

Hasura allows us to instantly create a real-time 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 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 would autogenerate the mutations. 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

mutation ($username: String!) {
  insert_user_one(object: { username: $username }) {
    id
    username
  }
}

We take the returned id and username and store them 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.

mutation ($userId: Int!) {
  update_user_by_pk(pk_columns: { id: $userId }, _set: { last_seen: "now()" }) {
    id
  }
}

Subscribing to new messages

Using streaming subscriptions we fetch the last N messages (the example uses 10). Then new messages are streamed using graphql-ws.

# We can pass in how far back we want to fetch messages
subscription ($last_received_ts: timestamptz) {
  message_stream(
    cursor: { initial_value: { timestamp: $last_received_ts } }
    batch_size: 10
  ) {
    id
    username
    text
    timestamp
  }
}

With a bit of magic from streaming subscriptions we saved our users a ton of data by only fetching new messages.

Sending messages

A user sends a message by inserting a row into the messages table. In the future if we setup JWT based authentication in the future we can automatically set the username from the JWT custom claims.

mutation insert_message($message: message_insert_input!) {
  insert_message_one(object: $message) {
    id
    timestamp
    text
    username
  }
}

UI Typing Indicator

  1. Similar to the user online events, when a user types a few characters we run a mutation on their last_typing timestamp.

    mutation ($userId: Int!) {
      update_user_by_pk(
        pk_columns: { id: $userId }
        _set: { last_typed: "now()" }
      ) {
        id
      }
    }
  2. Then we subscribe to our Postgres view of the last user typing, making sure to exclude ourselves.

    subscription ($selfId: Int) {
      user_typing(
        where: { id: { _neq: $selfId } }
        limit: 1
        order_by: { last_typed: desc }
      ) {
        last_typed
        username
      }
    }

Online Users list

We can easily get a list of all users online by subscribing to the user_online view we created.

subscription {
  user_online(order_by: { username: asc }) {
    id
    username
  }
}

streaming-subscriptions-chat's People

Contributors

arjunyel avatar lastnamearya avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

tsaidavid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.