Skip to content
This repository was archived by the owner on Feb 22, 2019. It is now read-only.

Commit b1c9b5e

Browse files
author
Gilles Ballini
authored
remove autoconnection from sdk code examples (#467)
# Conflicts: # src/guide/essentials/user-authentication.md # src/guide/getting-started/index.md # src/sdk-reference/essentials/offline-first.md # src/sdk-reference/kuzzle/connect.md # src/sdk-reference/kuzzle/index.md
1 parent 0cbdafb commit b1c9b5e

File tree

5 files changed

+83
-90
lines changed

5 files changed

+83
-90
lines changed

src/guide/essentials/user-authentication.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ If you're interested for a more in-depth explanation on how all of this work, th
6969

7070
## Authentication Strategies
7171

72-
Once a user has been created, they can access resources in Kuzzle as permitted by their security profile. However; in order to access these resources they will first need to identify & authenticate themselves using an authentication strategy. The authentication strategy defines what credentials are used and how Kuzzle should validate them. Kuzzle supports multiple authentication strategies, giving you more flexibility when building your security layer: use [Oauth](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth), Kerberos, Salesforce, and many more. And, if none of these suit your needs, follow our [Plugin Documentation]({{ site_base_path }}plugins-reference/plugins-features/adding-authentication-strategy) to learn how to build a custom authentication strategy.
72+
Once a user has been created, they can access resources in Kuzzle as permitted by their security profile. However; in order to access these resources they will first need to identify & authenticate themselves using an authentication strategy. The authentication strategy defines what credentials are used and how Kuzzle should validate them. Kuzzle supports multiple authentication strategies, giving you more flexibility when building your security layer: use [Oauth](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth), Kerberos, Salesforce, and many more. And, if none of these suit your needs, follow our [Plugin Documentation]({{ site_base_path }}plugins-reference/plugins-features/adding-authentication-strategy) to learn how to build a custom authentication strategy.
7373

7474
To request access to Kuzzle, a user must first send an [authentication request]({{ site_base_path }}api-documentation/controller-auth/login). Kuzzle will validate the credentials it receives in the request using the predefined authentication strategy and return a [JSON Web Token](https://tools.ietf.org/html/rfc7519) if the user credentials are valid.
7575

@@ -94,26 +94,25 @@ Then, let's create a `login.js` file that contains the following code:
9494
```javascript
9595
const Kuzzle = require('kuzzle-sdk')
9696

97-
var kuzzle = new Kuzzle('localhost', () => {
98-
kuzzle
99-
.loginPromise('local', {
100-
username: 'admin',
101-
password: 'test'
102-
})
103-
.then(() => {
104-
console.log('You are now logged in!')
105-
})
106-
.catch(err => {
107-
console.error(err.message)
108-
})
109-
})
97+
const kuzzle = new Kuzzle('localhost')
98+
99+
kuzzle
100+
.connectPromise()
101+
.then(() => loginPromise('local', {username: '<username>', password: '<password>'})
102+
.then(() => {
103+
console.log('logged!')
104+
})
105+
.catch(err => {
106+
console.error(err.message)
107+
})
110108
```
111109
112-
This code will:
110+
The code above does the following:
113111
114-
* load the Kuzzle Node.js SDK
115-
* connect to the Kuzzle
116-
* login using username `jondoe` and password `letmein`
112+
* loads the `Kuzzle` SDK from the NPM package,
113+
* instantiates a new SDK object
114+
* connects it to a remote Kuzzle server
115+
* performs a login with the provided username and password
117116
118117
Let's try it out! Run the `index.js` using Node.js:
119118

src/guide/getting-started/index.md

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,26 @@ Your `init.js` file should now look like this:
137137
// load the Kuzzle SDK module
138138
const Kuzzle = require('kuzzle-sdk')
139139

140-
// instantiate a Kuzzle client, this will automatically connect to the Kuzzle server
140+
// instantiate a Kuzzle client
141141
const kuzzle = new Kuzzle('localhost', {defaultIndex: 'playground'})
142142

143-
kuzzle.once('connected', () => {
144-
kuzzle
145-
.createIndexPromise('playground')
146-
.then(() => kuzzle.collection('mycollection').createPromise())
147-
.then(() => {
148-
console.log('playground/mycollection ready')
149-
})
150-
.catch(err => {
151-
console.error(err.message)
152-
})
153-
.finally(() => kuzzle.disconnect())
154-
})
143+
kuzzle
144+
.connectPromise()
145+
.then(() => kuzzle.createIndexPromise('playground'))
146+
.then(() => kuzzle.collection('mycollection').createPromise())
147+
.then(() => {
148+
console.log('playground/mycollection ready')
149+
})
150+
.catch(err => {
151+
console.error(err.message)
152+
})
153+
.finally(() => kuzzle.disconnect())
155154
```
156155

157156
This code does the following:
158-
* loads the `Kuzzle SDK` from its NPM package
159-
* creates an instance of the SDK and connects it to Kuzzle running on `localhost` (and selects the `playground` as default index),
157+
* loads the `Kuzzle` SDK from its NPM package,
158+
* creates an instance of the SDK,
159+
* connects the SDK to a remote Kuzzle server running on `localhost` (and selects the `playground` as default index),
160160
* creates the `playground` index,
161161
* creates the `mycollection` collection (within the `playground` index),
162162
* disconnects from Kuzzle after the collection is created or if an error occurs.
@@ -189,23 +189,22 @@ Create a `create.js` file with following code:
189189
// load the Kuzzle SDK module
190190
const Kuzzle = require('kuzzle-sdk')
191191

192-
// instantiate a Kuzzle client, this will automatically connect to the Kuzzle server
192+
// instantiate a Kuzzle client
193193
const kuzzle = new Kuzzle('localhost', {defaultIndex: 'playground'})
194194

195195
// create an object that contains the message we want to store
196196
const message = {message: "Hello, World!"}
197197

198-
kuzzle.once('connected', () => {
199-
kuzzle.collection('mycollection')
200-
.createDocumentPromise(message)
201-
.then(res => {
202-
console.log('the following document has been successfully created:\n', message)
203-
})
204-
.catch(err => {
205-
console.error(err.message)
206-
})
207-
.finally(() => kuzzle.disconnect())
208-
})
198+
kuzzle
199+
.connectPromise()
200+
.then(() => kuzzle.collection('mycollection').createDocumentPromise(message))
201+
.then(res => {
202+
console.log('the following document has been successfully created:\n', message)
203+
})
204+
.catch(err => {
205+
console.error(err.message)
206+
})
207+
.finally(() => kuzzle.disconnect())
209208
```
210209

211210
This code does the following:
@@ -241,7 +240,7 @@ Let's get started. Create a `subscribe.js` file with following code:
241240
// load the Kuzzle SDK module
242241
const Kuzzle = require('kuzzle-sdk')
243242

244-
// instantiate a Kuzzle client, this will automatically connect to the Kuzzle server
243+
// instantiate a Kuzzle client
245244
const kuzzle = new Kuzzle('localhost', {defaultIndex: 'playground'})
246245

247246
// create a reference to the 'mycollection' collection
@@ -255,10 +254,16 @@ const filter = {
255254
}
256255

257256
// create a subscription on the collection matching given filters
258-
collection.subscribe(filter, result => {
257+
collection
258+
.subscribe(filter, result => {
259259
// this function is called each time kuzzle notifies us with a document matching our filters
260-
console.log('message received from kuzzle:', result)
261-
})
260+
console.log('Message received from kuzzle:', result)
261+
})
262+
.onDone(() => console.log('Subscription active. Waiting for messages...'))
263+
264+
// Connects the client to a remote Kuzzle server and plays the previously
265+
// configured subscriptions
266+
kuzzle.connect()
262267
```
263268

264269
Run your file in Node.js

src/sdk-reference/essentials/offline-first.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ When using an unstable network connection, an application must maintain a normal
1515

1616
There are two ways to handle a network disconnect:
1717

18-
* Automatically reconnect to Kuzzle when possible, and enter *offline mode* in the meantime. This is the default behavior.
1918
* Stop all further communication with Kuzzle and invalidate the current instance and all its children. The application will have to manually reconnect once the network is available. To do so, simply set the ``autoReconnect`` option to ``false`` when creating the SDK instance.
19+
* Reconnect automatically to Kuzzle when possible, and enter *offline mode* in the meantime. This is the default behavior.
2020

2121
*Offline mode* refers to the time between a ``disconnected`` and a ``reconnected`` event (see [Events]({{ site_base_path }}sdk-reference/essentials/events)).
2222

@@ -42,7 +42,7 @@ The queue itself can be configured using the ``queueTTL`` and ``queueMaxSize`` o
4242

4343
---
4444

45-
## Filtering Requests to be Queued
45+
## Filter Requests to be Queued
4646

4747
By default, when queuing is first activated, all requests are queued.
4848

src/sdk-reference/kuzzle/connect.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,32 @@ title: connect
1111
# connect
1212

1313
```js
14-
kuzzle.connect();
14+
// Using callbacks (NodeJS or Web Browser)
15+
kuzzle.connect(function (err, kuzzle) {
16+
if (err) {
17+
console.log('Unable to connect: ', err.message);
18+
} else {
19+
console.log('Connected!');
20+
}
21+
});
22+
23+
// Using promises (NodeJS only)
24+
kuzzle.connectPromise()
25+
.then(() => console.log('Connected!'));
1526
```
1627

1728
```java
18-
kuzzle.connect();
29+
kuzzle.connect(new ResponseListener<Void>() {
30+
@Override
31+
public void onSuccess(Void object) {
32+
// invoked once connected
33+
}
34+
35+
@Override
36+
public void onError(JSONObject error) {
37+
// Handle connection error
38+
}
39+
});
1940
```
2041

2142
```php
@@ -24,17 +45,10 @@ kuzzle.connect();
2445
// not implemented (this SDK uses HTTP and is thus stateless)
2546
```
2647

27-
Connects to Kuzzle using the `host` parameter provided in the constructor.
28-
Has no effect if ``connect`` is set to ``auto``, unless ``disconnect`` has been called first.
29-
30-
---
31-
32-
## Return value
33-
34-
Returns the `Kuzzle` object to allow chaining.
48+
Connects to Kuzzle using the `host` and `port` parameters provided in the constructor.
3549

3650
---
3751

3852
## Callback Response
3953

40-
If a callback has been provided to the `Kuzzle` constructor, it will be called with the `Kuzzle` instance once connected to Kuzzle
54+
Resolves with nothing once connected to a remote Kuzzle server.

src/sdk-reference/kuzzle/index.md

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ var kuzzle = new Kuzzle('localhost', {
2020
autoReconnect: true,
2121
port: 7512
2222
});
23-
24-
// A callback is also available and will be invoked once connected to the Kuzzle instance:
25-
kuzzle = new Kuzzle('localhost', function (err, res) {
26-
// ...
27-
});
2823
```
2924

3025
```java
@@ -37,17 +32,7 @@ options.setDefaultIndex("some index")
3732
.setAutoReconnect(true),
3833
.setPort(7512);
3934

40-
Kuzzle kuzzle = new Kuzzle("localhost", options, new ResponseListener<Void>() {
41-
@Override
42-
public void onSuccess(Void object) {
43-
// invoked once connected, object contains the kuzzle instance
44-
}
45-
46-
@Override
47-
public void onError(JSONObject error) {
48-
// Handle connection error
49-
}
50-
});
35+
Kuzzle kuzzle = new Kuzzle("localhost", options);
5136
```
5237

5338
```php
@@ -66,13 +51,12 @@ This is the main entry point to communicate with Kuzzle. Every other object inhe
6651

6752
---
6853

69-
## Kuzzle(host, [options], [callback])
54+
## Kuzzle(host, [options])
7055

7156
| Arguments | Type | Description |
7257
|---------------|---------|----------------------------------------|
7358
| ``host`` | string | The server name (or the IP address) of a Kuzzle Backend installation |
7459
| ``options`` | JSON object | Optional Kuzzle connection configuration |
75-
| ``callback`` | function | Optional callback |
7660

7761
---
7862

@@ -84,7 +68,6 @@ This is the main entry point to communicate with Kuzzle. Every other object inhe
8468
| ``autoReconnect`` | boolean | Automatically reconnect after a connection loss | ``true`` |
8569
| ``autoReplay`` | boolean | Automatically replay queued requests on a ``reconnected`` event | ``false`` |
8670
| ``autoResubscribe`` | boolean | Automatically renew all subscriptions on a ``reconnected`` event | ``true`` |
87-
| ``connect`` | string | Manually or automatically connect to the Kuzzle instance | ``auto`` |
8871
| ``defaultIndex`` | string | Set the default index to use | |
8972
| ``offlineMode`` | string | Offline mode configuration | ``manual`` |
9073
| ``protocol`` | string | (Javascript only) Network protocol to use to connect to Kuzzle (``websocket`` | ``socketio``) | ``websocket``|
@@ -126,18 +109,10 @@ This is the main entry point to communicate with Kuzzle. Every other object inhe
126109

127110
**Notes:**
128111

129-
* if ``connect`` is set to ``manual``, the ``connect`` method will have to be called manually
130-
* the kuzzle instance will automatically queue all requests, and play them automatically once a first connection is established, regardless of the ``connect`` or offline mode option values.
112+
* newly instantiated Kuzzle objects automatically start in [offline mode]({{ site_base_path }}sdk-reference/essentials/offline-first/)
131113
* multiple methods allow passing specific ``volatile`` data. These ``volatile`` data will be merged with the global Kuzzle ``volatile`` object when sending the request, with the request specific ``volatile`` taking priority over the global ones.
132114
* the ``queueFilter`` property is a function taking a JSON object as an argument. This object is the request sent to Kuzzle, following the [Kuzzle API]({{ site_base_path }}api-documentation/query-syntax) format
133115
* if ``queueTTL`` is set to ``0``, requests are kept indefinitely
134116
* The offline buffer acts like a first-in first-out (FIFO) queue, meaning that if the ``queueMaxSize`` limit is reached, older requests are discarded to make room for new requests
135117
* if ``queueMaxSize`` is set to ``0``, an unlimited number of requests is kept until the buffer is flushed
136118
* the ``offlineQueueLoader`` must be set with a function, taking no argument, and returning an array of objects containing a `query` member with a Kuzzle query to be replayed, and an optional `cb` member with the corresponding callback to invoke with the query result
137-
138-
---
139-
140-
## Callback response
141-
142-
If the connection succeeds, resolves to the `Kuzzle` object itself.
143-
If the `connect` option is set to `manual`, the callback will be called after the `connect` method is resolved.

0 commit comments

Comments
 (0)