Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL subscription on same server throws error. Connection closed before receiving a handshake response. #915

Closed
ph55 opened this issue Jul 28, 2018 · 2 comments

Comments

@ph55
Copy link

ph55 commented Jul 28, 2018

Trying to configure GraphQL subscriptions using existing express server.

But seems that there is some kind of conflict.
Error thrown in graphiql console:

WebSocket connection to 'ws://localhost:3000/subscriptions' failed: Connection closed before receiving a handshake response

image

When using new server. There is no error.

Here the graphQL configuration I've used:
this.setSameServer() - uses nest http server instance.
this.setDifferentServer() - uses new http instance.

import {
  MiddlewareConsumer,
  Module,
  HttpServer,
  Inject,
  NestModule,
  OnModuleDestroy,
} from '@nestjs/common';
import { AppController } from 'app.controller';
import { AppService } from 'app.service';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { GraphQLModule, GraphQLFactory } from '@nestjs/graphql';
import { AuthorResolver } from 'author.resolver';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { execute, subscribe } from 'graphql';
import { createServer } from 'http';
import { HTTP_SERVER_REF } from '@nestjs/core';

@Module({
  imports: [GraphQLModule, AuthorResolver],
  controllers: [AppController],
  providers: [
    {
      provide: 'SUBSCRIPTION_SERVER',
      useFactory: () => {
        const server = createServer();
        return new Promise(resolve => server.listen(88, () => resolve(server)));
      },
    },
    AppService,
  ],
})
export class AppModule implements NestModule, OnModuleDestroy {
  private subscriptionServer: SubscriptionServer;
  private subscriptionPort: number;
  private wsServer: HttpServer;

  constructor(
    private readonly graphQLFactory: GraphQLFactory,
    @Inject(HTTP_SERVER_REF) private readonly httpServerRef: HttpServer,
    @Inject('SUBSCRIPTION_SERVER') private readonly ws: HttpServer,
  ) {
    this.setSameServer();
    //this.setDifferentServer();
  }

  private setSameServer() {
    this.wsServer = this.httpServerRef.getHttpServer();
    this.subscriptionPort = 3000;
  }

  private setDifferentServer() {
    this.wsServer = this.ws;
    this.subscriptionPort = 88;
  }

  public configure(consumer: MiddlewareConsumer) {
    const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
    const schema = this.graphQLFactory.createSchema({ typeDefs });

    const route = '/graphql';
    const routeIDE = '/graphiql';
    const routeSubs = '/subscriptions';

    const middlewareIDE = graphiqlExpress({
      endpointURL: route,
      subscriptionsEndpoint:
        'ws://localhost:' + this.subscriptionPort + routeSubs,
    });
    const middleware = graphqlExpress(req => ({
      schema,
      rootValue: req,
      debug: false,
    }));

    consumer.apply(middleware).forRoutes(route);
    consumer.apply(middlewareIDE).forRoutes(routeIDE);

    this.subscriptionServer = new SubscriptionServer(
      {
        execute,
        subscribe,
        schema,
      },
      {
        server: this.wsServer,
        path: routeSubs,
      },
    );
  }

  public onModuleDestroy() {
    this.subscriptionServer.close();
  }
}

Used these issues for help:
#500
nestjs/graphql#6

And full repo if you want to reproduce:
https://github.com/ph55/nest-graphql-subscriptions

@kamilmysliwiec
Copy link
Member

Please, use @nestjs/graphql repository to report this issue.

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants