Giter Site home page Giter Site logo

sls-websocket-demo's Introduction

Diagnostic Serverless Websocket Demo

Issue

When trying to respond to a WS message using the Aws.ApiGatewayManagementApi connector in serverless offline, the connector's TCP connection is refused. However this works fine when deployed to AWS.

Error Message:

Unable to generate socket message Error: connect ECONNREFUSED 127.0.0.1:443
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
  message: 'connect ECONNREFUSED 127.0.0.1:443',
  errno: 'ECONNREFUSED',
  code: 'NetworkingError',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 443,
  region: 'us-east-1',
  hostname: '',
  retryable: true,
  time: 2020-03-17T16:37:57.040Z
}
->StackTrace:
 Error: connect ECONNREFUSED 127.0.0.1:443
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)

Offending Code:

// See src/connector/apigateway.connector.js
class ApiGatewayConnector {
  constructor() {
    const CONNECTOR_OPTS = {
      apiVersion: '2018-11-29',
      endpoint: CONSTANTS.WEBSOCKET_API_ENDPOINT,
    };
    this._connector = new aws.ApiGatewayManagementApi(CONNECTOR_OPTS);
  }

  get connector() {
    return this._connector;
  }

  async generateSocketMessage(connectionId, data) {
    try {
      // This is the problem call where the Connection is refused
      // when sending a message to a WS client
      return await this._connector
        .postToConnection({
          ConnectionId: connectionId,
          Data: data,
        })
        .promise();
    } catch (error) {
      console.error('Unable to generate socket message', error);
      if (error.stack) {
        console.error('->StackTrace:\n', error.stack);
      }
      if (error.statusCode === 410) {
        console.log(`Removing stale connector ${connectionId}`);
      }
    }
  }
}

To replicate issue:

  1. yarn (...obviously)
  2. yarn offline - start sls offline
  3. Connect to ws://localhost:3001 (I use the Websocket King extension)
    This should connect fine
  4. Send a message. My test message is:
{
 "action":"PING"
}
  1. Sending this message will trigger the ECONNREFUSED error
  2. Can also simultate with yarn client in another terminal

Author

Bryan Vaz (@bryanvaz)

sls-websocket-demo's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.