cloudamqp / amqp-client.js

AMQP 0-9-1 TypeScript client both for Node.js and browsers (using WebSocket)
https://cloudamqp.github.io/amqp-client.js/
Apache License 2.0
200 stars 19 forks source link

Cannot Instantiate an AMQPClient Instance Because `name` is `undefined` #112

Closed ShuzhaoFeng closed 5 months ago

ShuzhaoFeng commented 5 months ago

Problem Description

Cannot instantiate an AMQPClient instance because name is undefined.

System Specs

OS: Windows 11 Pro 23H2 node version: v20.11.1 npm version: 10.5.2 npx version: 10.5.2 npx expo version: 0.17.10 Expo package version: 50.0.14 React package version: 18.2.0 React Native package version: 0.73.6

CloudAMQP Specs

Type: LavinMQ Active Plan: Loyal Lemming Region: amazon-web-services::ca-central-1 Cluster: rabbit.lmq.cloudamqp.com (DNS load balanced) Hosts: rabbit-01.lmq.cloudamqp.com(Availability Zone cac1-az4)

Example Code

https://github.com/ShuzhaoFeng/cloud-amqp-expo-test

Note that CloudAMQP credentials has been explicitly omitted before the push.

Steps to Reproduce (With Example Code)

  1. Clone the repository

  2. cd my-app

  3. npm run start

  4. Open the app on Expo and notice the error

Steps to Reproduce (Without Example Code)

  1. npx create-expo-app --template

  2. Select blank TypeScript template

  3. cd my-app

  4. npm i @cloudamqp/amqp-client

  5. In App.tsx, add the following import:

import { AMQPClient } from "@cloudamqp/amqp-client";
  1. In App.tsx, add the following lines at the beginning of the App() function:
  const cloud_amqp_url =
    "amqps://xxx:xxx@rabbit.lmq.cloudamqp.com/xxx";
  const client = new AMQPClient(cloud_amqp_url);
  1. npm run start

  2. Open the app on expo, and notice the following error:

Android Bundling failed 27945ms ({PATH_TO_PROJECT}\my-app\node_modules\expo\AppEntry.js)
The package at "node_modules\@cloudamqp\amqp-client\lib\cjs\amqp-socket-client.js" attempted to import the Node standard library module "net".
It failed because the native React runtime does not include the Node standard library.
Learn more: https://docs.expo.dev/workflow/using-libraries/#using-third-party-libraries
  1. Ctrl+C to stop the app

  2. npm i node-libs-react-native react-native-tcp stream-browserify stream

  3. npx expo customize metro.config.js

  4. In the newly Created metro.config.js file, add the following just above the line module.exports = config:

config.resolver.extraNodeModules["net"] = require.resolve("react-native-tcp");
config.resolver.extraNodeModules["tls"] = require.resolve(
  "node-libs-react-native/mock/tls"
);
  1. npm run start

  2. Open the app again and notice the error

Error Log

Android Bundled 3939ms ({PATH_TO_PROJECT}\my-app\node_modules\expo\AppEntry.js)      
 ERROR  TypeError: Cannot read property 'name' of undefined

This error is located at:
    in App (at withDevTools.js:18)
    in withDevTools(App) (at renderApplication.js:57)
    in RCTView (at View.js:116)
    in View (at AppContainer.js:127)
    in RCTView (at View.js:116)
    in View (at AppContainer.js:155)
    in AppContainer (at renderApplication.js:50)
    in main(RootComponent) (at renderApplication.js:67), js engine: hermes
 ERROR  TypeError: Cannot read property 'name' of undefined

This error is located at:
    in App (at withDevTools.js:18)
    in withDevTools(App) (at renderApplication.js:57)
    in RCTView (at View.js:116)
    in View (at AppContainer.js:127)
    in RCTView (at View.js:116)
    in View (at AppContainer.js:155)
    in AppContainer (at renderApplication.js:50)
    in main(RootComponent) (at renderApplication.js:67), js engine: hermes
dentarg commented 5 months ago

There's a lot going on here, are you able to reproduce the problem with a plain JavaScript or TypeScript program?

ShuzhaoFeng commented 5 months ago

There's a lot going on here, are you able to reproduce the problem with a plain JavaScript or TypeScript program?

While trying to reproduce, I realized that the issue comes from erroneously using AMQPClient instead of AMQPWebSocketClient for a non-node project. Additional modules (node-libs-react-native, react-native-tcp, stream, stream-browserify, text-encoding) are still needed to support amqp-client in an Expo React Native project.

I'll turn my example code shortly to a minimally reproducible example for using the module in an Expo React Native environment.

ShuzhaoFeng commented 3 months ago

Minimal example now available: https://github.com/ShuzhaoFeng/amqp-client-expo-example