首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用带有graphql订阅的socket.io?

如何使用带有graphql订阅的socket.io?
EN

Stack Overflow用户
提问于 2020-06-30 17:02:13
回答 1查看 983关注 0票数 3

我想通过使用nestjs和graphql技术制作一个实时聊天应用程序。大多数教程使用PubSub,但我不知道如何向特定的客户端发送消息(?)。我认为使用socket.io通过使用套接字id向特定客户端发送消息非常容易。

此示例使用PubSub:

  1. 有使用PubSub向特定客户端发送消息的方法吗?通过接收一些有关发件人的数据,如id.
  2. ,如何用Socket.io替换PubSub ?我对socket.io

感到非常满意。

App.module.ts

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
type Mutation {
    ping: Ping
}

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

type Ping {
  id: ID
}

type Pong {
  pingId: ID
}

如何用PubSub代替Socket.io?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-28 10:52:01

我没有找到从graphql订阅中获取客户端套接字的任何解决方案或示例。在大多数情况下,他们使用网关而不是graphql。因此,我使用GateWay来实现实时活动和针对其他请求的graphql。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62662561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档