node.jsrsocket

RSocketTcpClient is not a constructor - NodeJS with Rsocket-js


I would like to run Rsocket TCP client on NodeJs using simple example from Rsocket guide(https://rsocket.io/guides/rsocket-js/client/rsocket-tcp-client):

async function createClient(options) {
  const client = new RSocketClient({
    setup: {
      dataMimeType: 'text/plain',
      keepAlive: 1000000, // avoid sending during test
      lifetime: 100000,
      metadataMimeType: 'text/plain',
    },
    transport: new RSocketTCPClient({
      host: options.host,
      port: options.port,
    }),
  });

  return await client.connect();
}

async function run() {
  const rsocket = await createClient({
    host: '127.0.0.1',
    port: 9898,
  });
}

await run();

but when I am trying to run app by node app.js there is an error: TypeError: RSocketTcpClient is not a constructor. I've checked implementation of RSocketTcpClient, and it looks like there is constructor:

export class RSocketTcpClient extends RSocketTcpConnection {
  constructor(options: net.TcpSocketConnectOpts, encoders?: Encoders<any>);
  connect(): void;
}

Could anyone tell me what is wrong? I am using simple package.json:

{
  "name": "rsocket-example",
  "private": true,
  "description": "RSocket example",
  "version": "0.0.27",
  "repository": {
    "type": "git",
    "url": "https://github.com/rsocket/rsocket-js.git"
  },
  "type": "module",
  "dependencies": {
    "rsocket-core": "^0.0.27",
    "rsocket-flowable": "^0.0.27",
    "rsocket-tcp-server": "^0.0.27",
    "rsocket-types": "^0.0.27",
    "rsocket-websocket-client": "^0.0.27",
    "rsocket-websocket-server": "^0.0.27"
  }
}

Solution

  • The source project, rsocket-js in here, seems to have transpiration misconfigured letting the default export for the RSocketTCPClient class declaration go under a default variable.

    You workaround would then either to tweak the transpiler configuration in your own project or use just the default exported object as follows:

    // instead of new RSocketTCPClient()
    new RSocketTCPClient.default({
        host: options.host,
        port: options.port,
    });