繁体   English   中英

如何在快速应用程序中使用 websocket 进行 appsync graphql 订阅

[英]How to use websocket for appsync graphql subscription in express app

我正在尝试使用以下代码通过 websocket 收听 appsync graphql 订阅(onCreatePost):

import ws from 'ws';
import es6 from 'es6-promise';
import imf from 'isomorphic-fetch';
import { AUTH_TYPE, AWSAppSyncClient } from 'aws-appsync'
import dotenv from 'dotenv';
import gql from 'graphql-tag';
dotenv.config()

global.WebSocket
global.window = global.window || {
    setTimeout: setTimeout,
    clearTimeout: clearTimeout,
    WebSocket: global.WebSocket,
    ArrayBuffer: global.ArrayBuffer,
    addEventListener: function () { },
    navigator: { onLine: true }
};

global.localStorage = {
    store: {},
    getItem: function (key) {
        return this.store[key]
    },
    setItem: function (key, value) {
        this.store[key] = value
    },
    removeItem: function (key) {
        delete this.store[key]
    }
};

es6.polyfill();
imf

const client = new AWSAppSyncClient({
    url: process.env.AWS_APPSYNC_REALTIME_API_URL,
    region: process.env.AWS_REGION,
    auth:{
        type: AUTH_TYPE.API_KEY,
        apikey: process.env.AWS_APPSYNC_API_KEY
    }
});

const postsSub = gql(`
    subscription onCreatePost{
        onCreatePost{
            id
            content
            video
            createdAt
        }
    }
`)

client.hydrated().then(function (client){
    const observ = client.subscribe({ query: postsSub });
    const realTime = function realTime(data){
        console.log('Realtime Subscription to posts ----->', data);
    }

    observ.subscribe({
        next: realTime,
        complete: console.log,
        error: console.log
    })
});

将此用作参考: https ://thatcoder.space/aws-appsync-example-for-subscription-using-nodejs/

当我将该文件添加节点并从 appsync 控制台创建帖子时,什么也没有发生(文字),我做错了什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM