Skip to content

Commit f417961

Browse files
authored
Merge pull request #741 from kuzzleio/7-dev
Add getting started
2 parents 0a877eb + 78ee3e1 commit f417961

28 files changed

+1058
-0
lines changed

doc/7/getting-started/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
code: false
3+
type: branch
4+
title: Getting Started
5+
description: Get started with the Javascript SDK
6+
order: 0
7+
---
8+
9+
<RedirectToFirstChild />
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
code: false
3+
type: page
4+
title: Node.js
5+
description: Getting started with Kuzzle and Node.js
6+
order: 0
7+
---
8+
9+
# Getting Started with Kuzzle and Node.js
10+
11+
This tutorial explains you how to use **Kuzzle** with **Node.js** and the **Javascript SDK**.
12+
It will walk you through creating scripts that can **store** documents in Kuzzle and subscribe to **notifications** about document creations.
13+
14+
You are going to write an application that **stores** documents in Kuzzle Server and subscribe to **real time notifications** for each created document.
15+
16+
To follow this tutorial, you must have a Kuzzle Server up and running. Follow these instructions if this is not already the case: [Running Kuzzle](/core/2/guides/getting-started/run-kuzzle).
17+
18+
19+
:::info
20+
Having trouble? Get in touch with us on [Discord](http://join.discord.kuzzle.io)!
21+
:::
22+
23+
## Explore the SDK
24+
25+
It's time to get started with the [Kuzzle Javascript SDK](/sdk/js/7). This section, explains you how to store a document and subscribe to notifications in Kuzzle using the Javascript SDK.
26+
27+
Before proceeding, please make sure your system has **Node.js** version 8 or higher ([download page](https://nodejs.org/en/download/)) installed.
28+
29+
## Prepare your environment
30+
31+
Create your playground directory and install the Javascript SDK from the command line using npm:
32+
33+
```sh
34+
mkdir "kuzzle-playground"
35+
cd "kuzzle-playground"
36+
npm install kuzzle-sdk
37+
```
38+
39+
:::info
40+
If you are performing a clean install you might get some `UNMET PEER DEPENDENCY` warnings, these are safe to ignore as they refer to optional dependencies.
41+
:::
42+
43+
Then, create an `init.js` file and start by adding the code below.
44+
This loads the SDK and connects it to a Kuzzle instance using the WebSocket protocol.
45+
46+
<<< ./snippets/load-sdk.js
47+
48+
:::info
49+
Replace 'kuzzle' which is the Kuzzle server hostname with 'localhost' or with the host name where your Kuzzle server is running.
50+
:::
51+
52+
Next, add a listener to be notified in case of a connection error:
53+
54+
```js
55+
kuzzle.on('networkError', error => {
56+
console.error('Network Error: ', error);
57+
});
58+
```
59+
60+
Then, connect the client to your Kuzzle server with the `connect()` method, afterwards you have to add the code that will access Kuzzle to create a new index 'nyc-open-data' and a new collection 'yellow-taxi' that you will use to store data later on.
61+
62+
<<< ./snippets/prepare-db.js
63+
64+
Your `init.js` file should now look like this:
65+
66+
<<< ./snippets/init.js
67+
68+
This code does the following:
69+
70+
- loads the `Kuzzle SDK` from its NPM package
71+
- creates an instance of the SDK
72+
- connects it to Kuzzle running on `kuzzle` (change the hostname if needed) using WebSocket
73+
- creates the `nyc-open-data` index
74+
- creates the `yellow-taxi` collection (within the `nyc-open-data` index),
75+
- disconnects from Kuzzle after the collection is created or if an error occurs
76+
77+
Run the code with Node.js:
78+
79+
```bash
80+
node init.js
81+
```
82+
83+
The console should output the following message:
84+
85+
```bash
86+
nyc-open-data/yellow-taxi ready!
87+
```
88+
89+
:::success
90+
Congratulations! You are now ready to say Hello to the World!
91+
:::
92+
93+
## Create your first "Hello World" document
94+
95+
Create a `create.js` file with the following code:
96+
97+
<<< ./snippets/create.js
98+
99+
This code does the following:
100+
101+
- creates a new document in the `yellow-taxi` collection, within the `nyc-open-data` index
102+
- logs a success message to the console if everything went fine
103+
- logs an error message if any of the previous actions fails
104+
- disconnects from Kuzzle after the document is created or if an error occurs
105+
106+
Run the code with Node.js:
107+
108+
```bash
109+
node create.js
110+
```
111+
112+
:::success
113+
You have now successfully stored your first document into Kuzzle. You can now open an [Admin Console](http://console.kuzzle.io) to browse your collection and confirm that your document was saved.
114+
:::
115+
116+
117+
## Subscribe to realtime document notifications (pub/sub)
118+
119+
Kuzzle provides pub/sub features that can be used to trigger real-time notifications based on the state of your data (for a deep-dive on notifications check out the [realtime notifications](/sdk/js/7/essentials/realtime-notifications) documentation).
120+
121+
Let's get started. Create a `subscribe.js` file with the following code:
122+
123+
<<< ./snippets/subscribe.js
124+
125+
Run the code with Node.js:
126+
127+
```bash
128+
node subscribe.js
129+
```
130+
131+
The `subscribe.js` program is now running endlessly, waiting for notifications about documents matching its filters, specifically documents that have a `license` field equal to `'B'`.
132+
133+
Now in another terminal, launch the `create.js` file from the previous section.
134+
135+
```bash
136+
node create.js
137+
```
138+
139+
This creates a new document in Kuzzle which, in turn, triggers a [document notification](/core/2/api/payloads/notifications#document-notification) sent to the `subscribe.js` program.
140+
Check the `subscribe.js` terminal: a new message is printed everytime a document is created using the `create.js` code.
141+
142+
```bash
143+
New driver Sirkis with id AWccRe3-DfukVhSzMdUo has B license.
144+
```
145+
146+
:::success
147+
Congratulations! You have just set up your first pub/sub communication!
148+
:::
149+
150+
## Where do we go from here?
151+
152+
Now that you're more familiar with Kuzzle, dive even deeper to learn how to leverage its full capabilities:
153+
154+
- discover what this SDK has to offer by browsing other sections of this documentation
155+
- learn how to use [Koncorde](/core/2/api/koncorde-filters-syntax) to create incredibly fine-grained and blazing-fast subscriptions
156+
- learn how to perform a [basic authentication](/sdk/js/7/controllers/auth/login)
157+
- follow our guide to learn how to [manage users, and how to set up fine-grained access control](/core/2/guides/main-concepts/permissions)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Loads the Kuzzle SDK modules
2+
const {
3+
Kuzzle,
4+
WebSocket
5+
} = require('kuzzle-sdk');
6+
7+
// Instantiates a Kuzzle client with the WebSocket protocol
8+
// Replace 'kuzzle' with your Kuzzle server hostname (e.g. 'localhost')
9+
const kuzzle = new Kuzzle(
10+
new WebSocket('kuzzle')
11+
);
12+
13+
// Adds a listener to detect connection problems
14+
kuzzle.on('networkError', error => {
15+
console.error('Network Error:', error);
16+
});
17+
18+
const run = async () => {
19+
try {
20+
// Connects to the Kuzzle server
21+
await kuzzle.connect();
22+
23+
// Creates a document
24+
const driver = {
25+
name: 'Sirkis',
26+
birthday: '1959-06-22',
27+
license: 'B'
28+
};
29+
30+
await kuzzle.document.create('nyc-open-data', 'yellow-taxi', driver);
31+
console.log('New document successfully created!');
32+
} catch (error) {
33+
console.error(error.message);
34+
} finally {
35+
kuzzle.disconnect();
36+
}
37+
};
38+
39+
run();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: gettingstarted#nodejscreate
2+
description: Creates a document
3+
hooks:
4+
before: |
5+
curl -XPOST kuzzle:7512/nyc-open-data/_create
6+
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
7+
after:
8+
template: empty
9+
expected: New document successfully created!
10+
sdk: js
11+
version: 6
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Loads the Kuzzle SDK modules
2+
const {
3+
Kuzzle,
4+
WebSocket
5+
} = require('kuzzle-sdk');
6+
7+
// Instantiates a Kuzzle client with the WebSocket protocol
8+
// Replace 'kuzzle' with your Kuzzle server hostname (e.g. 'localhost')
9+
const kuzzle = new Kuzzle(
10+
new WebSocket('kuzzle')
11+
);
12+
13+
// Adds a listener to detect connection problems
14+
kuzzle.on('networkError', error => {
15+
console.error('Network Error:', error);
16+
});
17+
18+
const run = async () => {
19+
try {
20+
// Connects to the Kuzzle server
21+
await kuzzle.connect();
22+
23+
// Creates an index
24+
await kuzzle.index.create('nyc-open-data');
25+
26+
// Creates a collection
27+
await kuzzle.collection.create('nyc-open-data', 'yellow-taxi');
28+
29+
console.log('nyc-open-data/yellow-taxi ready!');
30+
} catch (error) {
31+
console.error(error.message);
32+
} finally {
33+
kuzzle.disconnect();
34+
}
35+
};
36+
37+
run();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: gettingstarted#nodejsinit
2+
description: Creates an index and a collection
3+
hooks:
4+
before: curl -X DELETE kuzzle:7512/nyc-open-data
5+
after:
6+
template: empty
7+
expected: nyc-open-data/yellow-taxi ready!
8+
sdk: js
9+
version: 6
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const {
2+
Kuzzle,
3+
WebSocket
4+
} = require('kuzzle-sdk');
5+
6+
// Replace 'kuzzle' with your Kuzzle server hostname (e.g. 'localhost')
7+
const kuzzle = new Kuzzle(
8+
new WebSocket('kuzzle')
9+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: gettingstarted#nodejsload
2+
description: Load SDK
3+
hooks:
4+
before:
5+
after:
6+
template: empty
7+
expected: Success
8+
sdk: js
9+
version: 6
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const run = async () => {
2+
try {
3+
// Connects to the Kuzzle server
4+
await kuzzle.connect();
5+
6+
// Creates an index
7+
await kuzzle.index.create('nyc-open-data');
8+
9+
// Creates a collection
10+
await kuzzle.collection.create('nyc-open-data', 'yellow-taxi');
11+
12+
console.log('nyc-open-data/yellow-taxi ready!');
13+
} catch (error) {
14+
console.error(error.message);
15+
} finally {
16+
kuzzle.disconnect();
17+
}
18+
};
19+
20+
run();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: gettingstarted#nodejspreparedb
2+
description: Prepare database
3+
hooks:
4+
before: curl -X DELETE kuzzle:7512/nyc-open-data
5+
after:
6+
template: blank
7+
expected: nyc-open-data/yellow-taxi ready!
8+
sdk: js
9+
version: 6

0 commit comments

Comments
 (0)