Home > Enterprise >  Getting 'No Living Connections' when trying to use elasticsearch in Nodejs via Firestore c
Getting 'No Living Connections' when trying to use elasticsearch in Nodejs via Firestore c

Time:11-12

I have a cloud function on firestore that looks like this:

const firebaseFunctions = functions.region("australia-southeast1").firestore;

const {Client} = require("elasticsearch");
const client = new Client({
  cloud: {
    id: env.elasticsearch.id,
  },
  auth: {
    username: env.elasticsearch.username,
    password: env.elasticsearch.password,
  },
});

exports.onBookingCreated = firebaseFunctions
    .document("bookings/{bookingId}")
    .onCreate(async (snap, context) => {
      const bookingData = snap.data();
      try {
        const response = await client.index({
          index: "all_bookings",
          id: context.params.bookingId,
          document: bookingData,
        });
        return response;
      } catch (err) {
        console.log(err.toString());
      }
    });

The above functions gets triggered but the indexing fails. I get the following the logs:

PS C:\Users\...\functions> firebase functions:log --only onBookingCreated
2022-11-09T00:39:42.120814Z ? onBookingCreated:       at HttpConnector.<anonymous> (/workspace/node_modules/elasticsearch/src/lib/connectors/http.js:171:7)
2022-11-09T00:39:42.120820Z ? onBookingCreated:       at ClientRequest.wrapper (/workspace/node_modules/lodash/lodash.js:4991:19)
2022-11-09T00:39:42.120825Z ? onBookingCreated:       at ClientRequest.emit (node:events:513:28)
2022-11-09T00:39:42.120844Z ? onBookingCreated:       at ClientRequest.emit (node:domain:489:12)
2022-11-09T02:43:38.852929Z ? onBookingCreated:       at Log.error (/workspace/node_modules/elasticsearch/src/lib/log.js:239:56)
2022-11-09T02:43:38.852934Z ? onBookingCreated:       at checkRespForFailure (/workspace/node_modules/elasticsearch/src/lib/transport.js:298:18)
2022-11-09T02:43:38.852940Z ? onBookingCreated:       at HttpConnector.<anonymous> (/workspace/node_modules/elasticsearch/src/lib/connectors/http.js:171:7)
2022-11-09T02:43:38.852945Z ? onBookingCreated:       at ClientRequest.wrapper (/workspace/node_modules/lodash/lodash.js:4991:19)
2022-11-09T02:43:38.852951Z ? onBookingCreated:       at ClientRequest.emit (node:events:513:28)
2022-11-09T02:43:38.852955Z ? onBookingCreated:       at ClientRequest.emit (node:domain:489:12)
2022-11-09T02:43:38.852960Z ? onBookingCreated:       at Socket.socketErrorListener (node:_http_client:481:9)
2022-11-09T02:43:38.852967Z ? onBookingCreated:       at Socket.emit (node:events:513:28)
2022-11-09T02:43:38.852972Z ? onBookingCreated:       at Socket.emit (node:domain:489:12)
2022-11-09T02:43:38.852977Z ? onBookingCreated:       at emitErrorNT (node:internal/streams/destroy:157:8)
2022-11-09T02:43:38.855848Z ? onBookingCreated: Elasticsearch WARNING: 2022-11-09T02:43:38Z
2022-11-09T02:43:38.855861Z ? onBookingCreated:   Unable to revive connection: http://localhost:9200/
2022-11-09T02:43:38.856087Z ? onBookingCreated: Elasticsearch WARNING: 2022-11-09T02:43:38Z
2022-11-09T02:43:38.856094Z ? onBookingCreated:   No living connections
2022-11-09T02:43:38.860774341Z D onBookingCreated: Function execution took 908 ms. Finished with status: ok
2022-11-09T02:43:39.900329Z ? onBookingCreated: Error: No Living connections

I am not hosting elastic search locally. I am using the cloud version. I install elasticsearch using npm install elasticsearch. My package.json looks like this:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "axiom": "^0.1.6",
    "axios": "^1.1.3",
    "elasticsearch": "^16.7.3",
    "express": "^4.18.2",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0",
    "request": "^2.88.2",
    "request-promise": "^4.2.6",
    "stripe": "^10.12.0-beta.1"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

I am pretty new to web/backend stuff so I am not really sure what's going on. I have searched everywhere. I see a lot of people mentioning the error however they are either using a different stack or they are hosting elasticsearch locally so their solutions didn't work for me. The error seems to suggest that elasticsearch is trying to establish a connection with http://localhost:9200/. I am not sure why that's happening since I am using my cloud id and credentials. Any help is greatly appreciated.

CodePudding user response:

You seem to be using a very old version of the elasticsearch client library. You now need to use the new one and install it like this

npm install @elastic/elasticsearch

And then your code will work.

  • Related