You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -10,37 +10,37 @@ A backend software, self-hostable and ready to use to power modern apps.
10
10
11
11
You can access the Kuzzle repository on [Github](https://github.com/kuzzleio/kuzzle)
12
12
13
+
This is the documentation for the beta-6 SDK. We are currently refactoring in deep our documentation to support many versions of each SDK. You can see it [here](https://docs-v2.kuzzle.io/sdk-reference/kuzzle/constructor/) but notice that this is a beta version of the documentation.
14
+
15
+
This new documentation will be available soon. In the meantime, you can still use the SDK 5 and the old documentation at https://docs.kuzzle.io/sdk-reference/essentials/.
The SDK supports different protocols. When instantiating, you must choose the protocol to use and fill in the different options needed to connect to Kuzzle.
27
29
28
-
The complete SDK documentation is available [here](http://docs.kuzzle.io/sdk-reference/)
The Javascript SDK implements two network protocols: raw WebSocket, and [Socket.IO](http://socket.io/)
33
-
The main reason behind this is that while Socket.IO offers better compatibility with older web browsers, our raw WebSocket implementation is about 20% faster
34
-
35
-
Which protocol is used when you connect to Kuzzle depends on multiple factors:
36
-
37
-
#### NodeJS
38
-
39
-
The protocol used is always raw WebSocket.
40
-
41
-
#### Web Browsers
42
-
43
-
The SDK will first try to use raw WebSocket to connect to Kuzzle. If the web browser does not support this protocol, then the SDK falls back to Socket.IO
36
+
kuzzle.connect()
37
+
.then(() => {
38
+
// You are now connected to your Kuzzle instance.
39
+
returnkuzzle.server.now();
40
+
})
41
+
.then(serverTime=>console.log(serverTime))
42
+
.catch(error=>console.error(error));
43
+
```
44
44
45
45
## Installation
46
46
@@ -50,19 +50,21 @@ This SDK can be used either in NodeJS or in a browser.
To run the SDK in the browser, you need to pick the [built version available here](https://raw.githubusercontent.com/kuzzleio/sdk-javascript/master/dist/kuzzle.js). You can also build it yourself by cloning this repository and running `npm run build`. A `dist` directory will be created, containing a browser version of this SDK.
59
+
To run the SDK in the browser, you need to pick the [built beta v6 version available here](https://raw.githubusercontent.com/kuzzleio/sdk-javascript/tree/6-beta/dist/kuzzle.js). You can also build it yourself by cloning this repository and running `npm run build`. A `dist` directory will be created, containing a browser version of this SDK.
If you want to support older browser versions, you may load `socket.io` before Kuzzle, making the SDK compatible with browsers without websocket support:
@@ -84,9 +86,204 @@ But you'll still need to pick the built version (which ships with the package).
Actually, the SDK support 3 protocols: `http`, `websocket` et `socketio`.
96
+
97
+
Websocket and Socket.IO protocols implement the whole Kuzzle API, while HTTP protocol does not implement realtime features (rooms and subscriptions).
98
+
While Socket.IO offers better compatibility with older web browsers, our raw WebSocket implementation is about 20% faster.
99
+
100
+
#### NodeJS
101
+
102
+
We recommend to use the `websocket` protocol, but you can still use `http`, `socketio` or even a custom protocol if you want.
103
+
104
+
#### Web Browsers
105
+
106
+
We also recommend to use the `webSocket` or `http` protocol, but some old browser may not support Websocket, so you have to implement a fallback to `socketio` in that case.
107
+
108
+
```js
109
+
let kuzzle;
110
+
111
+
if ('WebSocket'inwindow&&window.WebSocket.CLOSING===2) {
There are two ways to write these protocols, the first is to inherit the `KuzzleAbstractNetwork` class provided with the SDK and implement only the `connect` and `send` methods.
// (...) here the protocol-specific code to send the request to kuzzle and get the result into `result` variable
138
+
139
+
// Send back the result to SDK and resolve:
140
+
this.emit(request.requestId, { result });
141
+
returnPromise.resolve();
142
+
}
143
+
}
144
+
```
145
+
146
+
The second way is to implement the `isReady` and `query` methods as well as javascript [Event API](https://nodejs.org/api/events.html) by inheriting the `KuzzleEventEmitter` class.
// (...) here the protocol-specific code to send the request to kuzzle and get the result into `result` variable
159
+
160
+
// Resolves the response:
161
+
returnPromise.resolve({ result });
162
+
}
163
+
}
164
+
```
165
+
166
+
These customized protocols can then be used by the SDK by passing them as parameters at instantiation.
167
+
168
+
```js
169
+
constprotocol=newMyCustomProtocol();
170
+
171
+
constkuzzle=newKuzzle(protocol);
172
+
```
173
+
174
+
## SDK Documentation
175
+
176
+
### Connection to Kuzzle
177
+
178
+
By default, the SDK is not connected to Kuzzle when it is instantiated. You must manually call the `kuzzle.connect()` method before using the SDK.
179
+
180
+
It is then possible to interact with the Kuzzle API through the SDK once the Promise returned by `kuzzle.connect()` has been resolved.
181
+
182
+
```js
183
+
// Without async/await
184
+
kuzzle.connect()
185
+
.then(() => {
186
+
// You are now connected to your Kuzzle instance.
187
+
returnkuzzle.server.now();
188
+
})
189
+
.then(serverTime=>console.log(serverTime))
190
+
.catch(error=>console.error(error));
191
+
192
+
// With async/await
193
+
try {
194
+
awaitkuzzle.connect();
195
+
constserverTime=awaitkuzzle.server.now();
196
+
197
+
console.log(serverTime);
198
+
} catch (error) {
199
+
console.error(error);
200
+
}
201
+
```
202
+
203
+
### Match Kuzzle's API
204
+
205
+
The version 6 of this SDK involve a massive refactor of the SDK structure to match the [Kuzzle API](https://docs.kuzzle.io/api-documentation/connecting-to-kuzzle/).
206
+
207
+
Each controller is accessible from the Kuzzle object. The controller's actions are named in the same way as in the API.
208
+
209
+
For example, for the `create` action of the `document` controller ([document#create](https://docs.kuzzle.io/api-documentation/controller-document/create)):
The parameters of each method differ according to the parameters expected in the API.
217
+
If you want to get the details of the parameters for each method, it is necessary for the moment to see the code of each controller on [Github](https://github.com/kuzzleio/sdk-javascript/tree/6-beta/src/controllers).
218
+
219
+
#### Query method
220
+
221
+
This SDK also expose a low level `query` method to access the API even if the controller is not available inside the SDK.
222
+
223
+
This method take the controller and action name with all parameters needed by the action (`body`, `_id`, etc.) and return the raw Kuzzle response. This is the method used internally for every controller action in this SDK.
224
+
225
+
Example with the [Admin](https://docs.kuzzle.io/api-documentation/controller-admin/) controller:
All SDK methods return a promise resolving the `result` part of Kuzzle API responses. If an error occurs, the promise is rejected with an `Error` object embedding the `error` part of the API response.
263
+
264
+
For example, for the action `create` of the controller `collection` ([collection#create](https://docs.kuzzle.io/api-documentation/controller-collection/create)), the property `result` contains `{ "acknowledged": true} `. This is therefore what will be returned by the SDK method if successful.
265
+
266
+
Any error must be caught either at the end of the Promise chain, or by using `async/await` and a `try...catch`.
0 commit comments