|
| 1 | +--- |
| 2 | +type: page |
| 3 | +code: false |
| 4 | +title: Without Vuex |
| 5 | +description: Getting started with Kuzzle and VueJS |
| 6 | +order: 0 |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +# Getting Started with Kuzzle and VueJS |
| 11 | + |
| 12 | +This section deals with **Kuzzle** (+ **Javascript SDK**) and **VueJS**. We will create **documents** in Kuzzle and subscribe to [document notifications](/sdk/js/6/essentials/realtime-notifications/#document-messages) to develop a realtime chat. |
| 13 | + |
| 14 | +You can find the full code of this guide [here](https://github.com/kuzzleio/sdk-javascript/tree/6-dev/doc/6/getting-started/vuejs/without-vuex). |
| 15 | + |
| 16 | +## Requirements |
| 17 | + |
| 18 | +- **Node.js** >= 8.0.0 ([install here](https://nodejs.org/en/download/)) |
| 19 | +- **Vue CLI** ([install here](https://cli.vuejs.org/guide/installation.html)) |
| 20 | +- **Running Kuzzle Stack** ([instructions here](/core/1/guides/getting-started/running-kuzzle/)) |
| 21 | + |
| 22 | +## Prepare your environment |
| 23 | + |
| 24 | +Create your VueJS app with Vue CLI. You'll need to select manually the features, |
| 25 | +just add Babel, Linter and select the default options for the other features. |
| 26 | +```bash |
| 27 | +vue create kuzzle-playground |
| 28 | +``` |
| 29 | + |
| 30 | +Install the kuzzle-sdk: |
| 31 | +```bash |
| 32 | +cd kuzzle-playground |
| 33 | +yarn add kuzzle-sdk |
| 34 | +``` |
| 35 | + |
| 36 | +In the _App.vue_ file, you should remove the tag, the import and the component registration of the `HelloWorld` component, we won't use it. |
| 37 | + |
| 38 | +## Instanciating Kuzzle SDK |
| 39 | + |
| 40 | +We have to connect the server so that our client can interact with it. |
| 41 | + |
| 42 | +To do this, we have to create _src/services/kuzzle.js_ file to put our kuzzle instance, a bit like a singleton: |
| 43 | + |
| 44 | +<<< ./src/services/kuzzle.js |
| 45 | + |
| 46 | +We need to import our Kuzzle SDK instance, so just add the following line in your `<script>` tag in the _App.vue_ file: |
| 47 | + |
| 48 | +<<< ./src/App.vue:3[js] |
| 49 | + |
| 50 | +Then we will [etablish the connection](/sdk/js/6/core-classes/kuzzle/connect/) to kuzzle and create, if they don't [exists](sdk/js/6/controllers/index/exists/), the [index](sdk/js/6/controllers/index/create/) and [collection](sdk/js/6/controllers/collection/create/) of our chat. |
| 51 | +Add the following `valid()` method in the export of the `<script>` tag of your _App.vue_ file: |
| 52 | + |
| 53 | +<<< ./src/App.vue:2[js] |
| 54 | + |
| 55 | +## Get the username |
| 56 | +First of all, we need to know the user nickname. Let's start by adding the following html code in the `<template>` tag to allow the client type his nickname: |
| 57 | + |
| 58 | +<<< ./src/App.vue:1[html] |
| 59 | + |
| 60 | +As you can see we'll need two properties: `username` and `validate`. |
| 61 | + |
| 62 | + |
| 63 | +## Display the messages |
| 64 | + |
| 65 | +We'll need some properties to manage our messages. Add the following `data` to your _App.vue_ |
| 66 | + |
| 67 | +<<< ./src/App.vue:4[js] |
| 68 | + |
| 69 | +Then, create the following functions to fetch and display the messages: |
| 70 | + |
| 71 | +<<< ./src/App.vue:5[js] |
| 72 | + |
| 73 | +<<< ./src/App.vue:6[js] |
| 74 | + |
| 75 | +The function `fetch_message()` will [search](/sdk/js/6/controllers/document/search/) for the first hundred newest messages and store them in our array before subscribe to notification about `messages` collection. We called it in the `valid()` function we created before. |
| 76 | + |
| 77 | +<<< ./src/App.vue:7[js] |
| 78 | + |
| 79 | +Now, just add the following html code to display the messages: |
| 80 | + |
| 81 | +<<< ./src/App.vue:8[html] |
| 82 | + |
| 83 | +To finish this part, just add the following css classes: |
| 84 | + |
| 85 | +<<< ./src/App.vue:9[css] |
| 86 | + |
| 87 | +We can now display the messages stored in Kuzzle. Cool but we need to create some right ? |
| 88 | + |
| 89 | +## Send messages |
| 90 | + |
| 91 | +We need to write a simple method that will [create](/sdk/js/6/controllers/document/create/) a new message document in Kuzzle. |
| 92 | + |
| 93 | +<<< ./src/App.vue:10[js] |
| 94 | + |
| 95 | +As you can see we don't push the new message in our array on message creation. |
| 96 | +Actually, We'll subscribe to the collection that contains our messages. |
| 97 | +So let's create our `subscribe_messages()` action. It will call the realtime controller of Kuzzle to allow us to [receive notifications](/sdk/js/6/controllers/realtime/subscribe/) on message creation: |
| 98 | + |
| 99 | +<<< ./src/App.vue:11[js] |
| 100 | + |
| 101 | +To finish, just add an input field, bond to the `message` property and a button which calls our `sendMessage()` function: |
| 102 | + |
| 103 | +<<< ./src/App.vue:12[html] |
| 104 | + |
| 105 | +And the following CSS class: |
| 106 | + |
| 107 | +<<< ./src/App.vue:13[css] |
| 108 | + |
| 109 | +You can now add new messages to Kuzzle and receive the notification of the creation to update your state and display the new messages, enjoy :) |
| 110 | + |
| 111 | +You can find the full code of this guide [here](https://github.com/kuzzleio/sdk-javascript/tree/6-dev/doc/6/getting-started/vuejs/without-vuex). |
| 112 | + |
| 113 | +## Where do we go from here? |
| 114 | + |
| 115 | +Now that you're more familiar with Kuzzle, dive even deeper to learn how to leverage its full capabilities: |
| 116 | + |
| 117 | +- discover what this SDK has to offer by browsing other sections of this documentation |
| 118 | +- learn more about Kuzzle [realtime engine](/core/1/guides/essentials/real-time/) |
| 119 | +- follow our guide to learn how to [manage users, and how to set up fine-grained access control](/core/1/guides/essentials/security/) |
| 120 | +- lean how to use Kuzzle [Admin Console](/core/1/guides/essentials/admin-console/) to manage your users and data |
| 121 | +- learn how to perform a [basic authentication](/sdk/js/6/controllers/auth/login) |
0 commit comments