Pseudo input; true => Message input)
+ message: "", // String containing the user input
+ messages: [], // Array containing our messages
+ roomID: "", // Id of the realtime subscription
+ username: "", // Nickname of the current user
+ validate: false // Value that will change the display (false => Pseudo input; true => Message input)
};
},
-/* snippet:end */
+ /* snippet:end */
methods: {
-/* snippet:start:5 */
+ /* snippet:start:5 */
// This function return the right formated date depending on the timestamp
getDate(timestamp) {
const date = new Date(timestamp);
- return date.toString().split("GMT")[0];
+ return date.toLocaleString().split("GMT")[0];
},
-/* snippet:end */
-/* snippet:start:6 */
+ /* snippet:end */
+ /* snippet:start:6 */
// This function will create a message object containing the informations we need to display it
- getMessage(hit) {
+ getMessage(document) {
const message = {
// The unique id of the document containing the message
- _id: hit._id,
+ _id: document._id,
// The text of the message
- value: hit._source.value,
+ value: document._source.value,
// The creation date
- createdAt: hit._source._kuzzle_info.createdAt,
+ createdAt: document._source._kuzzle_info.createdAt,
// The author name
- username: hit._source.username
+ username: document._source.username
};
return message;
},
-/* snippet:end */
-/* snippet:start:10 */
+ /* snippet:end */
+ /* snippet:start:10 */
async sendMessage() {
if (this.message === "") return;
- // Call the create method of the document controller
- await kuzzle.document.create("chat", "messages",
+ // Call the create method of the document controller
+ await kuzzle.document.create(
+ "chat",
+ "messages",
// Give as parameter the object that will be store in kuzzle
{
value: this.message,
username: this.username
- });
+ }
+ );
// Clear the user input
this.message = "";
},
-/* snippet:end */
-/* snippet:start:11 */
- async subscribe_messages() {
- // Call the subscribe method of the realtime controller and receive the roomId
- // Save the id of our subscription (we could need it to unsubscribe)
- this.roomID = await kuzzle.realtime.subscribe(
- "chat", // Id of the index
- "messages", // Id of the collection
- {}, // Options
- // Callback for notifications receive
- notification => {
- // Check if the notification interest us (only document creation)
- if (
- notification.type === "document" &&
- notification.action === "create"
- ) {
+ /* snippet:end */
+ /* snippet:start:11 */
+ async subscribe_messages() {
+ // Call the subscribe method of the realtime controller and receive the roomId
+ // Save the id of our subscription (we could need it to unsubscribe)
+ this.roomID = await kuzzle.realtime.subscribe(
+ "chat", // Id of the index
+ "messages", // Id of the collection
+ {}, // Filter
+ // Callback for notifications receive
+ notification => {
+ // Check if the notification interest us (only document creation)
+ if (notification.type !== "document") return;
+ if (notification.action !== "create") return;
// Add the new message to our array
this.messages = [
this.getMessage(notification.result),
...this.messages
];
}
- });
+ );
},
/* snippet:end */
/* snippet:start:7 */