I am really confused..there is a new NPM project @elastic/elasticsearch - that code should just go in a new repo - why not just create a new repo elasticsearch.ts or elasticsearch.js and link to that from this one?
Anyway, regarding the new codebase, I am really happy to see that it's written in TypeScript, that's good news, but I have this problem/question:
import * as es from '@elastic/elasticsearch';
const client = new es.Client({node: 'http://ec2-xx-237-xx-107.us-west-2.compute.amazonaws.com:9200'});
client.once('connect', () => {
run(); // kiss
});
const run = () => {
client.indices.create({
index: 'foo'
})
.then(v => {
console.log(v);
});
};
it seems to connect, but the 'connect' event doesn't fire. This would be obvious feature that seems to be missing. However, if it's not too late, I recommend this API design instead:
import * as es from '@elastic/elasticsearch';
const client = new es.Client({node: 'http://ec2-xx-237-xx-107.us-west-2.compute.amazonaws.com:9200'});
const p = client.connect();
p.then(c => {
client.indices.create({
index: 'foo'
})
.then(v => {
console.log(v);
});
});
using promises for connections like that is a good use case for promises.
So the feature request here is to fire the 'connect' even upon a connection, and/or use a method called "connect()" which will return a promise.
Hello! Regarding the new client, I would suggest you read these two blog posts:
Regarding the "connect API", I'm not sure what you mean. As far as I know, there has never been a "connect API" both in the new and the legacy client.
Furthermore, we can discuss if it makes sense to have one since there is never a "connect event", because all the communications are done via HTTP and not TCP socket.
You can use the ping API to see if the cluster is reachable before to start sending requests.
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'https://example.com:9200' })
client.ping()
.then(() => {
return client.indices.create({ index: 'foo' })
})
.then(console.log)
.catch(console.error)
A final note about TypeScript: the client is not written in TypeScript, it is written in plain JavaScript, but it ships the type definitions with it.
@delvedor thanks, it would be nice to make the src of the library in TS, that way the types don't get out-of-sync. But anyway, I would expect this event to fire:
client.once('connect', () => {});
is there a good reason why there is no such event?
I believe that my message above already contains the answer.
Furthermore, we can discuss if it makes sense to have one since there is never a "connect event", because all the communications are done via HTTP and not TCP socket.
You can use the ping API to see if the cluster is reachable before to start sending requests.
There is never a "connect" event since we are using HTTP to talk with Elasticsearch, and thus, having a "connect" event is useless.
@delvedor ahhh of course, my mistake. On that note, is there a pure TCP API with a long-lived connection?
is there a pure TCP API with a long-lived connection?
Nope, but the client uses a Keep-Alive agent to avoid wasting time in multiple handshakings.
If needed, you can configure it with the agent option.
cool ty