microsoft / botbuilder-js

Welcome to the Bot Framework SDK for JavaScript repository, which is the home for the libraries and packages that enable developers to build sophisticated bot applications using JavaScript.
https://github.com/Microsoft/botframework
MIT License
682 stars 279 forks source link

Using Node.js with ASE did not work #4412

Closed compulim closed 1 year ago

compulim commented 1 year ago

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Versions

What package version of the SDK are you using. 4.14.1 What nodejs version are you using 18 What browser version are you using N/A What os are you using N/A

Describe the bug

Use DirectLineJS ASE or NodeWebSocket.connect() to connect to ASE or any Web Socket server.

To Reproduce

Steps to reproduce the behavior:

Using DirectLineJS

import { DirectLineStreaming } from 'botframework-directlinejs';

const TOKEN = '...';

async function main() {
  const token = await fetchToken();

  const connection = new DirectLineStreaming({
    domain: 'https://your-bot.azurewebsites.net/.bot/v3/directline',
    token: TOKEN
  });

  connection.connectionStatus$.subscribe(connectionStatus => {
    console.log(`Connection status: ${connectionStatus}`);
  });

  connection.activity$.subscribe(activity => {
    console.log(`Activity: ${activity.text}`);
  });
}

main();

Using NodeWebSocket.connect()

import { NodeWebSocket } from 'botframework-streaming';

async function main() {
  const socket = new NodeWebSocket();

  await socket.connect('wss://your-bot.azurewebsites.net/');
}

main();

Expected behavior

It should connect and will not throw any exceptions.

Screenshots

Using DirectLineJS

It should connect.

Failed to connect Error: Unable to connect client to Node transport.
/home/compulim/repos/experiment-dljs-ase-nodejs/node_modules/botframework-streaming/lib/webSocket/nodeWebSocketClient.js:63
                throw new Error(`Unable to connect client to Node transport.`);
                      ^

Error: Unable to connect client to Node transport.
    at WebSocketClient.<anonymous> (/home/compulim/repos/experiment-dljs-ase-nodejs/node_modules/botframework-streaming/lib/webSocket/nodeWebSocketClient.js:63:23)
    at Generator.throw (<anonymous>)
    at rejected (/home/compulim/repos/experiment-dljs-ase-nodejs/node_modules/botframework-streaming/lib/webSocket/nodeWebSocketClient.js:13:65)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Using NodeWebSocket.connect

It should not use URL as hostname. Because it is using the URL as the hostname, DNS cannot resolve wss://your-bot.azurewebsites.net/. Instead, it should resolve your-bot.azurewebsites.net.

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error: getaddrinfo ENOTFOUND wss://your-bot.azurewebsites.net/
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'wss://your-bot.azurewebsites.net/'
}

Additional context

NodeWebSocket.connect is not implemented correctly. Currently, it is:

  1. Create a Web Socket server (not client)
  2. Fake handshake
  3. Fake UPGRADE request
  4. Wait until the Web Socket is closed

It is broken in multiple ways:

I believe NodeWebSocket.connect never work and never tested properly.

orgads commented 1 year ago

@compulim I can now start the connection, but if I get an error, an unhandled exception is thrown from here, and I can't find a way to handle it. Looks similar to https://github.com/microsoft/BotFramework-DirectLineJS/issues/170.