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

萨尔巴兹·丹哈

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

此示例使用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 {}

聊天解析器

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);
    }
}

聊天图

type Mutation {
    ping: Ping
}

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

type Ping {
  id: ID
}

type Pong {
  pingId: ID
}

如何用Socket.io替换PubSub?

萨尔巴兹·丹哈

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在带有Visual Studio Code的TypeScript中使用Socket.IO?

来自分类Dev

过滤socket.io订阅

来自分类Dev

带有Express Generator的socket.io

来自分类Dev

使用Socket io航行js:在onCreate期间如何订阅符合条件的新模型?

来自分类Dev

Socket.io和Redis发布/订阅

来自分类Dev

带有Express的Socket.io,Express应用程序如何知道要监听的端口?

来自分类Dev

我如何使用 socket.io?

来自分类Dev

带有IO :: Socket :: SSL的SSL_ca_path在目录中不使用证书

来自分类Dev

带有querystring参数的AndroidAsync socket.io连接

来自分类Dev

带有承诺或异步回调的Socket.io

来自分类Dev

带有websocket的Node.js socket.io

来自分类Dev

带有Cordova的Socket.IO无法在设备上运行

来自分类Dev

带有Django和python socket io的eventlet

来自分类Dev

带有socket-io的Laravel事件[接收通知]

来自分类Dev

带有承诺或异步回调的Socket.io

来自分类Dev

带有Cordova的Socket.IO无法在设备上运行

来自分类Dev

socket.io如何工作

来自分类Dev

如何分发socket.io

来自分类Dev

使用VUE或vue-socket.io连接Socket.io

来自分类Dev

如何使用HTTPS从Node socket.io-client **连接到Node socket.io-server

来自分类Dev

Does Socket IO involve Disk IO?

来自分类Dev

如何在Socket.io/ SailsJS中订阅实例房间,其中id以外的属性为true?

来自分类Dev

如何在Socket.io/ SailsJS中订阅实例房间,其中id以外的属性为true?

来自分类Dev

具有socket.io的Mean.io框架

来自分类Dev

JavaScriptCore + Socket.IO

来自分类Dev

Socket.IO与扭曲

来自分类Dev

Socket.io替代

来自分类Dev

Ajax与Socket.io

来自分类Dev

JavaScriptCore + Socket.IO