如何通过 graphql 订阅使用 socket.io?
How to use socket.io with graphql subscription?
我想用nestjs和graphql技术做一个实时聊天的app。大多数教程使用 PubSub,但我不知道如何向特定客户端发送消息(?)。
我认为使用 socket.io 通过使用套接字 id 向特定客户端发送消息非常容易。
此示例使用 PubSub:
- 有没有办法使用 PubSub 向特定客户端发送消息?通过接收一些关于发件人的数据,比如 id。
- 如何用 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?
我没有找到任何解决方案或示例来从 graphql 订阅中获取 client-socket。
在大多数示例中,他们使用 Gateway 而不是 graphql pubsub。所以我使用 GateWay 来实现 real-time activities 和 graphql 用于其他请求。
我想用nestjs和graphql技术做一个实时聊天的app。大多数教程使用 PubSub,但我不知道如何向特定客户端发送消息(?)。 我认为使用 socket.io 通过使用套接字 id 向特定客户端发送消息非常容易。
此示例使用 PubSub:
- 有没有办法使用 PubSub 向特定客户端发送消息?通过接收一些关于发件人的数据,比如 id。
- 如何用 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?
我没有找到任何解决方案或示例来从 graphql 订阅中获取 client-socket。 在大多数示例中,他们使用 Gateway 而不是 graphql pubsub。所以我使用 GateWay 来实现 real-time activities 和 graphql 用于其他请求。