Amplify GraphQL subscriptions don't respond to events

1.9k views Asked by At

I'm currently building a React Native app using AWS Amplify framework.
In my app, there supposed to be a real-time dashboard that gets updated when a new item is added to the DB.
To achieve this functionality, I'm using GraphQL subscriptions using Amplify and AppSync API.

For some reason, the subscription won't respond to the events happening on the DB.

This is my code:

import React, { useEffect } from 'react';
import { API, graphqlOperation } from 'aws-amplify';
import * as subscriptions from '../../src/graphql/subscriptions';

const App = () => {
    useEffect(() => {
        subscribeUsers();
    }, []);

    const subscribeUsers = async () => {
        await API.graphql(graphqlOperation(subscriptions.onCreateUser, {appId: appId})).subscribe({
            next: (todoData) => console.log(todoData),
            error: (err) => console.log(err)
        }); 
    }

    ...
}

export default App;

This is my index.js code:

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import Amplify from 'aws-amplify';
import config from './aws-exports';

AppRegistry.registerComponent(appName, () => App);
Amplify.Logger.LOG_LEVEL = 'DEBUG';
Amplify.configure(config);

The debug log says that the connection was started:

AWSAppSyncRealTimeProvider - subscription message from AWS AppSync RealTime: {"id":"...","type":"start_ack"}

Also, every minute I get one of these log messages that tell me the connection is still alive, which is weird because I don't get information on any events happening:

AWSAppSyncRealTimeProvider - subscription message from AWS AppSync RealTime: {"type":"ka"}
AWSAppSyncRealTimeProvider {"id": "", "observer": null, "query": "", "variables": {}}

The GraphQL schema was generated directly by AppSync API.

Is there anything wrong with my subscription?

1

There are 1 answers

0
Ido Naveh On BEST ANSWER

Apparently, the reason I didn't get any updates from the subscription was because I tested it by adding a new item to the DB from the DynamoDB console, while AppSync subscriptions only work if the creating call is being made using the same AppSync API.