Does JS Driver Support `appName` in Connection String?

The JS code:

const url = process.env.MONGO_URL;
const mongoClient = await MongoClient.connect(url);

The .env file:

MONGO_URL="mongodb://_:<apiKey>@realm.mongodb.com:27020/?authMechanism=PLAIN&authSource=%24external&ssl=true&appName=<realm-app-id>:mongodb-atlas:api-key"

These codes cause the following error:

MongoServerSelectionError: failed to handle command request "ismaster": error processing client metadata: expected app name to be composed of: <appID>:<svc>:<authProvider>, got [object Object]

I think the error shows something wrong with the appName parameter.

I first thought it was a Realm problem, but the connection string properly works in CLI (i.e. the mongo command), so I’m now thinking this is related to JS Driver. (FYI: I’m using ver 3.6.2)

Is this an expected behavior or a bug?

Hi @Toshi,

The Node JS driver with latest versions should work with Realm wire protocols:

https://docs.mongodb.com/realm/mongodb/connect-over-the-wire-protocol/#compatible-clients

Do you see any server side logging for this attempts?

Best
Pavel

1 Like

Hi Pavel, thanks for the response. Firstly, I can’t find any logs recorded in the Realm UI unfortunately.

Actually, you can reproduce the problem in a couple of minutes:

# in empty directory
npm init -y
npm install mongodb
touch index.js

index.js:

const MONGO_URL = "URL COMES HERE"
const MongoClient = require('mongodb');

const main = async () => {
  await MongoClient.connect(MONGO_URL);
  console.log('connected!');
}

main().catch(err => {
  console.error(err);
});

If you use a normal Atlas url, it works properly. (i.e. mongodb+srv://<username>:<password>@cluster0.xxxx.mongodb.net/....)

On the other hand, if you use a Realm url, you’ll see the
expected <appID>:<svc>:<authProvider>, got [object Object]” error.

I’d appreciate it if you could check.

UPDATE:
I tried several more attempts with some guesswork (still not working).
I guess there is possibly something wrong with dealing with colons.


1. Using the string copied form Realm UI (“Data Source” page)

The connection string which has

appName=application-0-xxxxx:mongodb-atlas:custom-token

will produce this error:
expected ... <appID>:<svc>:<authProvider>, got [object Object]


2. Using URL encoded one (colon → %3A)

appName=application-0-xxxxx%3Amongodb-atlas%3Acustom-token

will produce the same error:
expected ... <appID>:<svc>:<authProvider>, got [object Object]


3. Using a plain string without colons

appName=application-0-xxxxx

This time the param was not converted to an [object Object].
The error message says it got the string instead:
expected ... <appID>:<svc>:<authProvider>, got application-0-xxxxx

Again, the connection string correctly worked with CLI (the mongo command). It seems it is not working only with Node.js SDK.