Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    JavaScript

    Agile Software Development, Scrum part 2

    Beginners

    Everything You Wanted to Know about the UX Research Methods

    Python

    Build a Python Command-line Program Using OOP

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Tuesday, September 9
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / Vue / Real-Time Subscriptions with Vue and GraphQL
    GraphQL

    Real-Time Subscriptions with Vue and GraphQL

    EmmanuelBy EmmanuelDecember 16, 2020No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Real-Time Subscriptions with Vue and GraphQL
    Real-Time Subscriptions with Vue and GraphQL
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Real-Time Subscriptions with Vue and GraphQL
    Real-Time Subscriptions with Vue and GraphQL

    Introduction

    Prior to the introduction of GraphQL subscriptions, making real-time data manipulation has been a bit of a hassle due to the massive lines of code involved in setting up and using web sockets to fetch data in real-time. Now with GraphQL subscriptions, it is easier to fetch data in real-time. The Hasura engine makes building GraphQL backends seemingly easy without writing any GraphQL backend code unless you want to add some custom business logic to your GraphQL backend.
    In this tutorial, we will move from setting up and deploying GraphQL backend with Hasura cloud to consuming the subscription endpoint on the vue.js application by building a project. Also, we will discover how to test GraphQL subscriptions on the Hasura console.

    Pre-requisite

    1. npm/yarn
    2. node 10+ running.
    3. Basic understanding of Vue.js
    4. Knowledge of JavaScript ES6
    5. Basic knowledge of GraphQL

    What will you build?

    We are going to build the SoccerStarsNomination application together. This application allows its users to nominate a football star for an award.
    Our application will make calls to a GraphQL subscription API endpoint to retrieve our nominations in real-time. We’ll then harness the GraphQL mutation to create a new nomination.

    Setting up Backend APIs on Hasura Cloud

    Hasura Cloud is a GraphQL service provider that gives you a fully managed, scalable, production-ready, and secured GraphQL API as a service to help you build modern apps faster.
    Signing up with Hasura Cloud is totally free and It does not require a credit card unless you want to upgrade.

    To create a new project on Hasura Cloud, click here.
    Once you register and sign in, you should see the welcome screen.

    If you have an existing Hasura Cloud account, You will be directed to your dashboard. On the dashboard, click the New project link and fill the form as follows:

    Image of Hasura cloud project setup 1
    Image of Hasura cloud project setup 1

    Image of Hasura cloud project setup 1

    Click the Continue to Database Setup button. Then select the Try with Heroku option. Click on the Heroku button and your Heroku database URL will be generated automatically as seen below:

    Image of Hasura cloud project setup 2
    Image of Hasura cloud project setup 2

    Image of Hasura cloud project setup 2Finally, click on the create project button to create your GraphQL backend project. When the process is successful, your dashboard should be as follows:

    Image of Hasura cloud successful project setup
    Image of Hasura cloud successful project setup

    Image of Hasura cloud successful project setupClick on the Launch Console button to open the Hasura Console to get started.

    Your Hasura console should look like this:

    Image of the Hasura console
    Image of the Hasura console

    Now, we have deployed our GraphQL backend service with Hasura cloud and the admin console is ready to get started.

    Creating Data Model for our GraphQL backend

    Our Application will be dependent on a single model called nominations.
    Let’s get started by creating the nominations table with the following columns:

    • id
    • name
    • current_club
    • nationality
    • no_of_clubs
    • created_at

    Navigate to data and click the create table button and create nominations table as follows:

    Image of Hasura console tables
    Image of Hasura console tables

    Image of Hasura console tablesClick on the Add Table button to create a table once you are done filling the form.

    Explore real-time nominations on GRAPHIQL API via Subscriptions

    subscription {
      nominations {
        id
        name
        nationality
        no_of_clubs
        current_club
      }
    }

    Copy and paste the code above on the GRAPHIQL editor as follows:

    Image of GraphiQL editor with subscription query
    Image of GraphiQL editor with subscription query

    Image of GraphiQL editor with subscription queryThen clicks the play button to see the output.
    As seen on the right side of the GRAPHIQL editor above, the result of this subscription is an empty array. This is because we have not added the appropriate data to the nomination table yet.
    We will handle this in the frontend section of this tutorial.

    Read More:  Server-Side Rendering with Vue.js and Nuxt.js

    Setting up the Frontend of our Project

    I have the initial project set up already so you can clone the project here.
    The initial project contains the static content of the application that we will build and the basic user interface of our real-time nomination app. The user interface is built with bootstrap 4 and some CSS styles.

    Inside the project directory, run the npm install command to install the existing dependencies in the package.json file
    next, Spin up the vue.js server with the npm run serve command

    Image of the initial project UI
    Image of the initial project UI

    Image of the initial project UI

    Dependency Installation

    Our real-time nomination app requires the following dependencies:

    • apollo-cache-inmemory: is the recommended cache implementation for Apollo Client 2.0.
    • apollo-client:  is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL.
    • apollo-link-ws: Send GraphQL operations over a WebSocket. Works with GraphQL Subscriptions.
    • graphql: is the JavaScript reference implementation for GraphQL APIs.
    • graphql-tag: is a JavaScript template literal tag that parses GraphQL queries.
    • subscriptions-transport-ws:  is a GraphQL WebSocket server and client to facilitate GraphQL queries, mutations and subscriptions over WebSocket.
    • vue-apollo:  is used to integrate GraphQL in Vue.js apps.

    Install the dependencies above with the following command:

    npm install --save vue-apollo graphql apollo-client apollo-link-ws apollo-cache-inmemory graphql-tag subscriptions-transport-ws

    Updating main.js

    Now we need to update our ApolloClient instance to point to the subscription server.
    Open src/main.js and replace its content with the code snippet below:

    import Vue from "vue";
    import App from "./App.vue";
    import router from "./router";
    import "bootstrap";
    import "bootstrap/dist/css/bootstrap.min.css";
    import VueApollo from "vue-apollo";
    import ApolloClient from "apollo-client";
    import { WebSocketLink } from "apollo-link-ws";
    import { InMemoryCache } from "apollo-cache-inmemory";
    Vue.use(VueApollo);
    // Create a WebSocket link:
    const link = new WebSocketLink({
      uri: "wss://graphql-api.hasura.app/v1/graphql",
      options: {
        reconnect: true,
        timeout: 30000,
      }
    });
    const client = new ApolloClient({
      link: link,
      cache: new InMemoryCache({
        addTypename: true
      })
    });
    Vue.config.productionTip = false;
    const apolloProvider = new VueApollo({
      defaultClient: client
    });
    new Vue({
      router,
      apolloProvider,
      render: h => h(App)
    }).$mount("#app");
    

    Here, we create a webSocketLink instance and configure its URL to our single GraphQL API endpoint which points to the subscription server. Next, we create an apolloClient instance and configure its link to the webSocketLink instance. Next, we create an vueApollo instance and configure its defaultClient property to the apolloClient instance. Finally, we add ApolloProvider to the Vue instance so that the Apollo client instances can then be used by all the child components.

    Update the Initial Components

    The initial project setup contains two components which are:

    • NominationBoard
    • NominationModal

    NominationBoard Component

    This component displays the list of football superstars nominated for the most versatile footballer award in a table.

    Open src/components/NominationBoard.vue and replace its content with the code snippet below:

    <template>
        <div>
            <div v-if="loading" class="text-center">
                <img  src="../assets/loading.svg" alt="loader" />
            </div>
            <div v-else>
            <table class="table table-hover">
                <thead class="bg-dark text-white">
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Name</th>
                    <th scope="col">Current Club</th>
                    <th scope="col">No_of_clubs</th>
                    <th scope="col">Nationality</th>
                </tr>
                </thead>
                <tbody>
                
                <tr v-for="nomination in nominations" :key="nomination.id">
                    <th scope="row">{{ nomination.id }}</th>
                    <td>{{ nomination.name }}</td>
                    <td>{{ nomination.current_club }}</td>
                    <td>{{ nomination.no_of_clubs }}</td>
                    <td>{{ nomination.nationality }}</td>
                </tr>
                </tbody>
            </table>
            </div>
        </div>
    </template>
    <script>
    import gql from "graphql-tag";
     const SUBSCRIPTION_NOMINATION = gql`
       subscription  getNominations {
        nominations(order_by: { created_at: desc }) {
          id
          name
          nationality
          no_of_clubs
          current_club
          created_at
        }
      }
     `;
    export default {
      data: function() {
        return {
            loading: true,
          nominations: [],
        }
      },
      apollo: {
       // Subscriptions
       $subscribe: {
         // When a nomination is added
         nominations: {
           query: SUBSCRIPTION_NOMINATION,
           // Result hook
           result (data) {
               console.log(data)
             // Let's update the local data
             this.nominations = data.data.nominations
             this.loading = false
           },
         },
       },
     },
    }
    </script>

    The code snippet above defines the subscription query to get the nominations created from the Hasura GraphQL server we built earlier in real-time. This means that whenever a new nomination is created, the UI will immediately update with the new nomination without sending any request to the Hasura GraphQL server.
    Vue-apollo allows us to define smart subscriptions that retrieve data in real-time from the subscription endpoint to the components state.

    Read More:  Handling GraphQL API Authentication using Auth0 with Hasura Actions

    NominationModal Component

    This component displays a form to create a nomination.

    Open src/components/NominationModal.vue and replace its content with the code snippet below:

    <template>
      <div>
        <!-- Button trigger modal -->
        <button
          type="button"
          class="btn nominate-btn btn-primary"
          data-toggle="modal"
          data-target="#exampleModal"
        >
          Nominate superstar
        </button>
        <!-- Modal -->
        <div
          class="modal fade"
          id="exampleModal"
          tabindex="-1"
          role="dialog"
          aria-labelledby="exampleModalLabel"
          aria-hidden="true"
        >
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button
                  type="button"
                  class="close"
                  data-dismiss="modal"
                  aria-label="Close"
                >
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <form>
                  <div class="form-group">
                    <label for="exampleInputName">Superstar's Name</label>
                    <input
                      type="text"
                      class="form-control"
                      id="exampleInputName"
                      placeholder="Enter name"
                      v-model="superstar_name"
                    />
                  </div>
                  <div class="form-group">
                    <label for="exampleInputClub">Current Club</label>
                    <input
                      type="text"
                      class="form-control"
                      id="exampleInputClub"
                      placeholder="Current club"
                      v-model="current_club"
                    />
                  </div>
                  <div class="form-group">
                    <label for="exampleInputNationality">Nationality</label>
                    <input
                      type="text"
                      class="form-control"
                      id="exampleInputNationality"
                      placeholder="Nationality"
                      v-model="nationality"
                    />
                  </div>
                  <div class="form-group">
                    <label for="exampleInputClubNo">No of Clubs</label>
                    <input
                      type="number"
                      min="1"
                      max="20"
                      class="form-control"
                      id="exampleInputClubNo"
                      v-model="no_of_clubs"
                    />
                  </div>
                </form>
              </div>
              <div class="modal-footer">
                <button
                  type="button"
                  class="btn btn-secondary"
                  data-dismiss="modal"
                >
                  Close
                </button>
                <button
                  type="button"
                  class="btn btn-primary"
                  @click="addNomination"
                >
                  Save changes
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>
    <script>
    import gql from "graphql-tag";
    const ADD_NOMINATION = gql`
      mutation insert_nominations(
        $name: String!
        $nationality: String!
        $no_of_clubs: Int!
        $current_club: String!
      ) {
        insert_nominations(
          objects: {
            name: $name
            nationality: $nationality
            no_of_clubs: $no_of_clubs
            current_club: $current_club
          }
        ) {
          affected_rows
          returning {
            id
            name
            nationality
            no_of_clubs
            current_club
            created_at
          }
        }
      }
    `;
    export default {
      data() {
        return {
          superstar_name: "",
          current_club: "",
          nationality: "",
          no_of_clubs: "",
        };
      },
      methods: {
        addNomination: function () {
          // insert new nomination into db
          const name = this.superstar_name && this.superstar_name.trim();
          const currentClub = this.current_club && this.current_club.trim();
          const nationality = this.nationality && this.nationality.trim();
          const noOfClubs = this.no_of_clubs && this.no_of_clubs.trim();
          this.$apollo.mutate({
            mutation: ADD_NOMINATION,
            variables: {
              name: name,
              nationality: nationality,
              no_of_clubs: noOfClubs,
              current_club: currentClub,
            },
            update: (cache, { data: { insert_nominations } }) => {
              // Read the data from our cache for this query.
              // eslint-disable-next-line
              console.log(insert_nominations);
            },
          });
          this.superstar_name = "";
          this.current_club = "";
          this.nationality = "";
          this.no_of_clubs = "";
        },
      },
    };
    </script>
    

    Here, we create a mutation to add a new nomination to the database. We made use of variables to factor dynamic values out of the query. Next, in the addNomination function, the $apollo.mutate method allows us to trigger a call to the ADD_NOMINATION mutation with all the variables being passed.

    Now you can run the application with:

    npm run serve

    Conclusion

    In this tutorial, we have moved from setting up and deploying GraphQL backend with Hasura cloud to consuming the subscription endpoint on the vue.js application.
    I hope you have learned a great deal from this tutorial. Do reach out in the comment section below if you have any questions or suggestions.

    Here is the Github repo for the final version of our project.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Emmanuel

      Related Posts

      Server-Side Rendering with Vue.js and Nuxt.js

      February 26, 2024

      An Introduction to Pinia: The Alternative State Management Library for Vue.js Applications

      March 29, 2023

      How To Use GraphQL with Flutter

      February 22, 2023
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Recruitment December 18, 2024

      Ensuring Quality Hires: The Vital Role of Background Checks

      Ensuring quality hires is crucial for organizational success. Background checks play a vital role in this process, helping employers verify credentials, assess potential risks, and create a trustworthy workforce, ultimately enhancing overall company performance.

      Essential Steps for Effective Project Risk Assessment

      November 29, 2024

      Express.js Lessons. Logger, Configuration, Templating with EJS. Part 2.

      December 2, 2016

      8. Уроки Node.js. Наследование от ошибок Error

      September 12, 2016

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Overcoming the Challenges of B2B Lead Generation Today

      B2B Leads November 27, 2024

      Python map() Function Explained & Visualized

      Programming October 7, 2019

      Top 10 issues when hiring freelancers

      Remote Job September 28, 2018

      Safeguarding Innovation: A Guide to Startup Intellectual Property

      Startups December 16, 2024

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Development

      Implementing Data Privacy Principles in Software Development

      Node.js

      Node.js Lesson 9: Events, EventEmitter and Memory Leaks

      JavaScript

      Analyzing Leadership Styles and Their Influence on Project Success

      Most Popular

      30 React JS Tools That You Can Use

      Beginners

      Why Most MSPs Fail at Lead Generation (And How to Fix It)

      Lead Generation

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      Finance & Fintech
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.