如何在 GraphQL 订阅中使用 Socket.IO?

3
我希望使用Nestjs和GraphQL技术创建一个实时聊天应用。大部分教程都使用PubSub,但我不知道如何向特定的客户端发送消息(?)。 我认为使用socket.id可以更轻松地使用socket.io向特定的客户端发送消息。
这个示例使用了PubSub:
  1. 有没有一种方法可以使用PubSub向特定的客户端发送消息?通过接收有关发送者的一些数据,例如id。
  2. 如何用Socket.io替换PubSub? 我非常喜欢使用socket.io。
App.module.ts
import { Module, MiddlewareConsumer, RequestMethod, Get } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeOrmConfig } from './config/typeorm.config';
import { AuthModule } from './auth/auth.module';
import { LoggerMiddleware } from './common/middlewares/logger.middleware';
import { Connection } from 'typeorm';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { ChatModule } from './chat/chat.module';
import { ChatService } from './chat/chat.service';

@Module({
  imports: [
    TypeOrmModule.forRoot(typeOrmConfig), // connect to database
    GraphQLModule.forRoot({
      debug: true,
      playground: true,
      typePaths: ['**/*.graphql'],
      definitions: {
        path: join(process.cwd(), 'src/graphql.ts'),
      },
    }),
    AuthModule,
    ChatModule,
  ],
  controllers: [],
  providers: [
  ],
})
export class AppModule {
}

Chat.module.ts

import { Module } from '@nestjs/common';
import { ChatService } from './chat.service';
import { ChatResolver } from './chat.resolver';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from '../auth/auth.module';
import { PubSub } from 'graphql-subscriptions';

@Module({
  imports: [
    // Add all repository here; productRepository, ...
    TypeOrmModule.forFeature( [
      // repositories ...
    ]),
    AuthModule
  ],
  providers: [
     //=> How to replace with socket.io ?
     {
       provide: 'PUB_SUB',
       useValue: new PubSub(),
    },
    ChatService, 
    ChatResolver,
  ]
})
export class ChatModule {}

Chat.resolver.ts

import { Resolver, Query, Mutation, Args, Subscription } from '@nestjs/graphql';
import { PubSubEngine  } from 'graphql-subscriptions';
import { Inject } from '@nestjs/common';

const PONG_EVENT_NAME = 'pong';

@Resolver('Chat')
export class ChatResolver {
    constructor(
        private chatService: ChatService,

        @Inject('PUB_SUB') 
        private pubSub: PubSubEngine,
    ) {}

    // Ping Pong
    @Mutation('ping')
    async ping() {
        const pingId = Date.now();
        //=> How to send deta to specific client by using user-id?
        this.pubSub.publish(PONG_EVENT_NAME, { ['pong']: { pingId } });
        return { id: pingId };
    }

    @Subscription(PONG_EVENT_NAME)
    pong() {
        //=> how to get data about sender like id?
        return this.pubSub.asyncIterator(PONG_EVENT_NAME);
    }
}

Chat.graphql

type Mutation {
    ping: Ping
}

type Subscription {
  tagCreated: Tag
  clientCreated: Client
  pong: Pong
}

type Ping {
  id: ID
}

type Pong {
  pingId: ID
}

如何使用Socket.io替换PubSub?

1个回答

0

我没有找到任何解决方案或示例来获取GraphQL订阅的客户端套接字。 在大多数示例中,他们使用网关而不是GraphQL PubSub。因此,我使用了GateWay来实现实时活动,并使用GraphQL处理其他请求。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接