|
| 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) |
0 commit comments