moleculerjs/moleculer-apollo-server

Service crash after close browser tab with active subscription

Closed this issue · 1 comments

I have few services with qraphql subscription.
As example record service with graphql subscription:

  actions: {
    addNew: {
      graphql: {
        input: `input NewRecordInput {
          title: String!
         }
        `,
        mutation: `addNewRecord(input: NewRecordInput!): Record`
      },
      async handler(ctx) {
        const {user} = ctx.meta;
        const { title} = ctx.params.input;
        const record = {
          title,
          user: user._id,
          createdAt: Date.now(),
          type: 'inbox',
        };
        try {
        const result = await this.adapter.collection.insert(record);
          const newRecord = result.ops[0];
          await this.broker.broadcast('graphql.publish', {
            tag: 'RECORD.INBOX',
            payload: { data: {_id:newRecord._id.toString() }},
          });
          return newRecord;
        } catch(err) {
          console.log(err);
          throw err;
        }
      }
    },
     'new.inbox.event': {
      params: { payload: 'object' },
      graphql: {
        type: `type IdPart{
         _id: ID,
        }`,
        subscription: 'newInboxRecord: IdPart',
        tags: ['RECORD.INBOX'],
      },
      handler(ctx) {
        return ctx.params.payload.data;
      },
    },
  },

But sometimes after closing client on browser tab with active subscription core web-service with ApiGateway and ApolloService mixins crash with error:

node_modules/ws/lib/websocket.js:757
  websocket.readyState = WebSocket.CLOSING;
                       ^

TypeError: Cannot set property 'readyState' of undefined
    at Socket.socketOnClose (/node_modules/ws/lib/websocket.js:757:24)
    at Socket.emit (events.js:327:22)
    at Socket.EventEmitter.emit (domain.js:485:12)
    at TCP.<anonymous> (net.js:674:12)

#51
Sorry for dublicate