Building a Minimal Chat System with Hasura Using GraphQL in Flutter

Building a Minimal Chat System with Hasura Using GraphQL in Flutter

In this detailed guide we learn how to build a responsive chat system using GraphQL subscriptions in Flutter


7 min read

In today's rapidly evolving technological landscape, the ability to create a minimal chat system has emerged as a fundamental skill for developers across various domains.

The importance of a well-designed chat system cannot be overstated, whether it's facilitating quick discussions among team members, providing customer support in real-time, or enabling social interaction among users.

This blog will guide you through building a simple chat application using Hasura, a real-time GraphQL engine.

Steps to Follow

  • What is GraphQL and subscription

  • Import GraphQL packages from

  • create GraphQL client

  • Bloc state management

  • How do we implement a chat screen in which users can chat with each other?

Step 1: What is GraphQL and Subscriptions?

GraphQL makes getting data more accessible because clients can ask for what they need. Subscriptions let you see updates in real time by keeping a constant connection between your device and the server.

Step 2: Import GraphQL Package

To get started, import the GraphQL Flutter package from This powerful package seamlessly integrates GraphQL queries, mutations, and subscriptions into your Flutter app.

Screenshot 2024-01-16 at 12.58.38 PM.png

Step 3: Create GraphQL Client

Creating a GraphQL client in this project is necessary to efficiently fetch and manage data from a GraphQL server. It ensures the app can communicate effectively with the server, request specific data, and receive real-time updates, providing a seamless user experience.

Set up a GraphQL client with HTTP and WebSocket links, connecting it to your server URL.

import 'package:graphql_flutter/graphql_flutter.dart';

class GraphQLService {
  static final GraphQLService _graphqlService = GraphQLService._internal();

  factory GraphQLService() {
    return _graphqlService;


  /// GraphQL client to handle queries and mutations operations
  GraphQLClient client({String? uri, Map<String, String>? headers}) {
    Map<String, String> defaultHeaders = {};

    defaultHeaders.addAll(headers ?? {});

    HttpLink link = HttpLink(
      uri ?? "http://<IP ADDRESS OF SERVER URL>:<PORT NO.>/v1/graphql",
      defaultHeaders: defaultHeaders,

    final WebSocketLink websocketLink = WebSocketLink(
      'ws://<IP ADDRESS OF SERVER URL>:<PORT NO.>/v1/graphql',
      config: SocketClientConfig(
        autoReconnect: true,
        headers: headers,
        inactivityTimeout: const Duration(seconds: 30),

    Link currentLink =
        Link.split((request) => request.isSubscription, websocketLink, link);

    final client = GraphQLClient(
      link: currentLink,
      cache: GraphQLCache(
        store: InMemoryStore(),
      defaultPolicies: DefaultPolicies(

        watchQuery: Policies(fetch: FetchPolicy.cacheAndNetwork),
        watchMutation: Policies(fetch: FetchPolicy.cacheAndNetwork),
        query: Policies(fetch: FetchPolicy.cacheAndNetwork),
        mutate: Policies(fetch: FetchPolicy.cacheAndNetwork),

    return client;

Step 4: Bloc State Management

Bloc state management in Flutter helps keep the user interface separate from the business logic by using Events, Blocs, and States. This makes the code easier to understand and maintain, and it's great for building bigger Flutter apps because it handles state changes efficiently.

In our app, we made a bloc like the one below.

Message Bloc:

The MessageBloc class manages messages in a Flutter app, connecting it to a GraphQL server. It retrieves messages and sends new ones, using variables like offset and limit for fetching messages in parts for implementing pagination. When fetching, it shows loading signs to keep users updated. If successful, it shows success, or failure with error messages. The _sendMessage function reliably sends messages, handling both success and failure scenarios.

Overall, the MessageBloc ensures smooth communication between users, managing messages effectively. Its features like pagination and error handling improve the user experience by making it more seamless and reliable.

  • message_bloc.dart

class MessageBloc extends Bloc<MessageBlocEvent, MessageBlocState> {
  QueryResult<Mutation_SENDMESSAGE>? sendMessage;
  List<Object> messages = [];
  int offset = 0;
  int limit = 20;
  bool isLoading = false;

  MessageBloc() : super(MessageBlocInitialState()) {

  FutureOr<void> _getMessage(
      GetMessage event, Emitter<MessageBlocState> emit) async {
    try {
      if (event.firstTime) {
        offset = 0;
      } else {
        isLoading = true;

      //fetch all messages according to offset
      QueryResult<Query_GET_All_MESSAGE> message =
          await event.client.query_GET_All_MESSAGE(
          variables: Variables_Query_GET_All_MESSAGE(
              chat_id: event.chatId, offset: offset, limit: limit),

      if ( != null) {

        if (event.firstTime) {
          messages = message.parsedData?.chat_messages ?? [];
        } else {
          messages.addAll(message.parsedData?.chat_messages ?? []);

        if (message.parsedData?.chat_messages.isNotEmpty == true) {
          offset = offset + limit;
        isLoading = false;

      } else {
        isLoading = false;

        if ( == null) {
              error: isUserUpdated.exception?.graphqlErrors[0].message ??
                  "Failed to update user status"));
        } else {
              error: message.exception?.graphqlErrors[0].message ??
                  "Failed to fetch messages"));
    } catch (e) {
      isLoading = false;

      emit(MessageBlocFailedState(error: e.toString()));


  // Event handler to send a message
  FutureOr<void> _sendMessage(SendMessage event, Emitter<ChatBlocState> emit) async {
    try {
      sendMessage = await event.client.mutate_SENDMESSAGE(
          variables: Variables_Mutation_SENDMESSAGE(
            chatId: event.chatId,
            chatMessage: event.message,
            senderId: event.senderId,

      if (sendMessage?.data != null) {
      } else {
          error: sendMessage?.exception?.graphqlErrors[0].message ?? "Failed to send message",
    } catch (e) {
      emit(SendMessageFailedState(error: e.toString()));


class MessageBlocEvent {}

// Event to get messages
class GetMessage extends MessageBlocEvent {
  final GraphQLClient client;
  final String chatId;

  final bool firstTime;

      {required this.client, required this.chatId, required this.firstTime});

// Event to send a message
class SendMessage extends ChatBlocEvent {
  final String chatId;
  final String message;
  final String senderId;
  final GraphQLClient client;

    required this.chatId,
    required this.message,
    required this.senderId,
    required this.client,


class MessageBlocState {}

class MessageBlocInitialState extends MessageBlocState {}

class MessageBlocLoadingState extends MessageBlocState {}

class MessageBlocSuccessState extends MessageBlocState {}

class MessageBlocFailedState extends MessageBlocState {
  final String error;
  MessageBlocFailedState({required this.error});

//send message
class SendMessageSuccessState extends ChatBlocState {}

class SendMessageFailedState extends ChatBlocState {
  final String error;
  SendMessageFailedState({required this.error});

Step 5: How Do You Implement a Chat Screen in Which Users Can Chat with Each Other?

Query: This Query we are using for fetching previous messages

query GET_All_MESSAGE($chat_id: uuid!, $offset: Int!, $limit: Int!) {
  chat_messages(where: {chat_id: {_eq: $chat_id}}, limit: $limit, order_by: {created_at: desc}, offset: $offset) {

Mutation: This mutation will be used on sending messages

mutation SENDMESSAGE($chatId: String!, $chatMessage: String!, $senderId: String!) {
  sendmessage(input: {senderId: $senderId, chatMessage: $chatMessage, chatId: $chatId}) {

Subscription: This Subscription will be use for fetching last message of chat room with input as chat id

static String fetchMessages = '''
  subscription ChatMessages(){
  chat_messages(order_by: {created_at: desc}, limit: 1) {

We add a listener to the _scrollController to detect when the user reaches the top of the screen, indicating a need for previous chats. Scrolling up triggers the event to fetch previous messages, ensuring smooth navigation and access to chat history.

EVENT: GetMessage

To implement pagination, the firstTime parameter in the GetMessage event controls message retrieval. When set to true, it fetches the first 20 messages, and when set to false, it fetches the next 20 previous messages, enabling smooth navigation and loading of chat history in the app.

  void initState() {


    chatBloc = BlocProvider.of<ChatBloc>(context);
    authBloc = BlocProvider.of<AuthBloc>(context);
    messageBloc = BlocProvider.of<MessageBloc>(context);

        client: authBloc.state, chatId: chatId, firstTime: true));

  void dispose() {

  void _scrollListener() {
    if (_scrollController.position.pixels == 0) {

      firstTime = false;
          client: authBloc.state, chatId: chatId, firstTime: false));

      setState(() {});

This is how you can use subscription for getting realtime message updates of this chat room

        options: SubscriptionOptions(
            document: gql(fetchMessages),
            variables: {"chat_id": widget.chatId}),
        builder: (QueryResult result) {
        if (["chat_messages"].isNotEmpty == true) {
        } else if (result.hasException) {
          // Handle subscription errors
          return Center(
            child: Text(
                'Subscription Error: ${result.exception?.graphqlErrors[0].message}'),
        // reverse a list when we are displaying so that latest message should come at last message of screen.
        return ListView.builder(
                    controller: _scrollController,

                    shrinkWrap: true,
                        const AlwaysScrollableScrollPhysics(),
                    itemBuilder: (context, index) {

        // this checks,who send this message and get type by comparing of our user id
                      String type = messageBloc
                                  .sender_id ==
                          ? "sender"
                          : "reciever";

        // this formattedDate we are using in display dates between the messages
                      String? formattedDate;
                      if ((index - 1) > 0) {
                        DateTime current = DateTime.parse(

                        DateTime next = DateTime.parse(
                                .toList()[index - 1]

                        if ( != {
                          formattedDate =
                              DateFormat('E, d MMM yyyy')
                      } else if (index == 0) {
                        formattedDate =
                            DateFormat('E, d MMM yyyy').format(

                  //return chat bubble component which you can use to display
                  // item of this list, making like this if it is of type reciever align left if sender align right in a screen


If a message is received from the subscription, it's added to the list only if it's not already there. This prevents duplicates and ensures new messages are included in the existing list seamlessly.

void addNewMessageToList(Query_GET_All_MESSAGE_chat_messages message) {
    List<Query_GET_All_MESSAGE_chat_messages> currentMessages =

    messageBloc.messages = [];

    bool isExist = currentMessages.any((element) => ==;

    if (!isExist) {

      Future.delayed(Duration(milliseconds: 50), () {
          duration: Duration(milliseconds: 300),
          curve: Curves.easeInOut,




Using GraphQL subscriptions in a Flutter chat app means instant updates without refreshing. It's like getting notifications for new messages, making chatting quick and effortlessly responsive.

This article was written by Sumit Soni, Software Engineer - I, for the GeekyAnts blog.