Connecting to Aerospike NodeJs client

I am trying to connect to the cluster like in the tutorial:

https://www.aerospike.com/apidocs/nodejs/tutorial-node_clusters.html

  client.connect((err) => { if (err) throw err })
  http.createServer((req, res) => {
  debug('incoming request on worker %s', cluster.worker.id)
  var key = keyFromPath(req)
  var responder = sendResponse.bind(null, res)
  switch (req.method) {
    case 'GET':
      client.get(key, responder)

but I seem to get the error:

AerospikeError: Not connected.

Is there a way to use the client without a callback / promise of the connect function?

I am hoping to be able to bootstrap it at the startup and just use it as a constant and close the connection at the end of the script.

Hi,

I first tried to replicate your issue using this simplified script:

const Aerospike = require('aerospike')
const client = new Aerospike.Client({
  hosts: 'localhost:32838'
})
client.connect((error) => { if (error) throw error })

setTimeout(async () => {
  const key = new Aerospike.Key('test', 'test', 'test')
  await client.put(key, { str: 'abc' })
  const result = await client.get(key)
  console.log(result)
  client.close()
})

That works as expected. So I tried to run the example code from the tutorial you linked. I’ve pulled the whole code out of that tutorial together into one single script here: node_clusters.js · GitHub.

On start, each worker connects to the Aerospike cluster as expected:

$ AEROSPIKE_HOSTS=localhost:32838 NODE_DEBUG=server node node_clusters.js
SERVER 37523: worker 1 starting
SERVER 37524: worker 2 starting
SERVER 37523: Connected to Aerospike cluster
SERVER 37524: Connected to Aerospike cluster
SERVER 37525: worker 3 starting
SERVER 37526: worker 4 starting
SERVER 37525: Connected to Aerospike cluster
SERVER 37526: Connected to Aerospike cluster
SERVER 37523: incoming request on worker 1

Maybe you can try to enable debug logs, to verify that the client is trying to connect to the right address, e.g.

const Aerospike = require('aerospike')
const client = new Aerospike.Client({
  hosts: 'localhost:32838',
  log: {
    level: Aerospike.log.DEBUG,
    file: process.stdout.fd
  }
})
client.connect((error) => { if (error) throw error })

setTimeout(async () => {
  const key = new Aerospike.Key('test', 'test', 'test')
  await client.put(key, { str: 'abc' })
  const result = await client.get(key)
  console.log(result)
  client.close()
})

Running this script, you should see something like this:

$ node test.js
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [config.cc:234] [config_from_jsobject] - Parsing global policies: success
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [config.cc:322] [config_from_jsobject] - Built as_config instance from JS config object
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [client.cc:80] [New] - Aerospike client initialized successfully
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [events.cc:57] [push] - Cluster event 0 triggered by node "BB9040011AC4202" ([::1]:32838)
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [events.cc:57] [push] - Cluster event 0 triggered by node "BB9030011AC4202" (192.168.1.11:32835)
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [client.cc:99] [Connect] - Successfully connected to cluster: Enjoy your cake!
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [command.cc:66] [Callback] - Executing JS callback for Connect command
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [put_async.cc:73] [PutAsync] - Sending async put command
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [command.cc:66] [Callback] - Executing JS callback for Put command
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [get_async.cc:56] [GetAsync] - Sending async get command
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [command.cc:66] [Callback] - Executing JS callback for Get command
Record {
  key: Key {
    ns: 'test',
    set: 'test',
    key: 'test',
    digest: <Buffer ee d5 e3 61 f7 0d c4 ab 19 71 3f c0 f6 87 74 62 8c a8 32 f6>
  },
  bins: { str: 'abc' },
  ttl: -1,
  gen: 14
}
Jul 08 2020 01:25:38 UTC: DEBUG(43516) [client.cc:114] [Close] - Closing the connection to aerospike cluster

Thank you for the quick reply! Quite strange. I’ll just post some snippets:

const client = new Aerospike.Client(config)
client.connect((error) => { if (error) throw error });

console.log("Client is connected:", client.isConnected())

And here is the console output:

Getting Aerospike client

Jul 08 2020 07:22:08 UTC: DEBUG(87410) [config.cc:234] [config_from_jsobject] - Parsing global policies: success
Jul 08 2020 07:22:08 UTC: DEBUG(87410) [config.cc:322] [config_from_jsobject] - Built as_config instance from JS config object
Jul 08 2020 07:22:08 UTC: DEBUG(87410) [client.cc:80] [New] - Aerospike client initialized successfully
Jul 08 2020 07:22:08 UTC: DEBUG(87410) [config.cc:234] [config_from_jsobject] - Parsing global policies: success
Jul 08 2020 07:22:08 UTC: DEBUG(87410) [config.cc:322] [config_from_jsobject] - Built as_config instance from JS config object
Jul 08 2020 07:22:08 UTC: DEBUG(87410) [client.cc:80] [New] - Aerospike client initialized successfully
Jul 08 2020 07:22:08 UTC: DEBUG(87410) [events.cc:57] [push] - Cluster event 0 triggered by node "BB989D03A184512" (10.227.56.172:3000)
Jul 08 2020 07:22:09 UTC: DEBUG(87410) [client.cc:99] [Connect] - Successfully connected to cluster: Enjoy your cake!
Jul 08 2020 07:22:09 UTC: DEBUG(87410) [command.cc:66] [Callback] - Executing JS callback for Connect command
Client is connected: false
(node:87410) UnhandledPromiseRejectionWarning: AerospikeError: Not connected.

From the debug logs it seems to connect to the cluster:

Jul 08 2020 07:22:09 UTC: DEBUG(87410) [client.cc:99] [Connect] - Successfully connected to cluster: Enjoy your cake!

Later edit.

It seems that connect function is async. It works with:

await client.connect((error) => { if (error) throw error });

Thank you so much for your help :slight_smile:

It seems that connect function is async.

It is! Even though the connection logic in the underlying Aerospike C client is synchronous, the communication of the connection status back into the JS scope is asynchronous. That’s why client.isConnected() will report that it’s not, even though the connection to the cluster is already established, unless you await the connection result.

Glad you managed to solve the problem!