From f4dfb652decf3dbaa3d5720dbdb2373aa3402414 Mon Sep 17 00:00:00 2001 From: Andrea Sponziello Date: Mon, 21 Jun 2021 22:54:41 +0200 Subject: [PATCH] enhanced testing --- benchmarks/direct.js | 94 +++++++++++++++++ benchmarks/groups.js | 162 ++++++++++++++++++++++++++++++ chatdb/index.js | 2 +- chatservermq.js | 5 +- mqttclient/chat21client.js | 29 +++--- observer.js | 16 +-- package-lock.json | 201 ++++++++++++++----------------------- package.json | 5 +- test/test.js | 67 +++++++------ webhooks/index.js | 3 +- 10 files changed, 402 insertions(+), 182 deletions(-) create mode 100644 benchmarks/direct.js create mode 100644 benchmarks/groups.js diff --git a/benchmarks/direct.js b/benchmarks/direct.js new file mode 100644 index 0000000..086e43b --- /dev/null +++ b/benchmarks/direct.js @@ -0,0 +1,94 @@ +var assert = require('assert'); +const { Chat21Client } = require('../mqttclient/chat21client.js'); + +const user1 = { + userid: 'USER1', + fullname: 'User 1', + firstname: 'User', + lastname: '1', + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2NzI5NDc4ZS1mOWIwLTRiODctYjNhYS03ZjU1OWExNzc5YjIiLCJzdWIiOiJVU0VSMSIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjEuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIxLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjEiLCJjaWQiOiJVU0VSMSIsImF6cCI6IlVTRVIxIiwidXNlcl9pZCI6IlVTRVIxIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjEiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.r-GBXo1fIUtl1QjOkXxcRaenVNQBElRkus3omh9YtjQ' +}; + + const user2 = { + userid: 'USER2', + fullname: 'User 2', + firstname: 'User', + lastname: '2', + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0MmEwMjc2ZC1lODUzLTQ5YjMtOTU4ZS0xODBkMjFjZGZjNWMiLCJzdWIiOiJVU0VSMiIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjIuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIyLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjIiLCJjaWQiOiJVU0VSMiIsImF6cCI6IlVTRVIyIiwidXNlcl9pZCI6IlVTRVIyIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjIiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.Zkbr3e9MfGGDKRdVUyG4330LxeNaKYS0y3upPtS4Wgg' +}; + +const user3 = { + userid: 'USER3', + fullname: 'User 3', + firstname: 'User', + lastname: '3', + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlMmI2Y2RhMi0yNjhmLTQxZDMtYjBjYy1kZWNjN2I0M2UwMjEiLCJzdWIiOiJVU0VSMyIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjMuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIzLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjMiLCJjaWQiOiJVU0VSMyIsImF6cCI6IlVTRVIzIiwidXNlcl9pZCI6IlVTRVIzIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjMiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.-Cio8ITPCQswv_4KnxJrRbm-5RCXMefuT91wWUNZJmU' +}; + +const MQTT_ENDPOINT = 'ws://localhost:15675/ws'; +const APPID = 'tilechat'; + +let TOTAL_MESSAGES = 1; +let starttime; +let endtime; + +startDirectBenchmark(); + +function startDirectBenchmark() { + console.log("Sending " + TOTAL_MESSAGES + " direct messages from 1 user to 1 other user. Delivering a total of " + TOTAL_MESSAGES + " messages."); + let messages_count = 0; + let chatClient1 = new Chat21Client( + { + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT + } + ); + let chatClient2 = new Chat21Client( + { + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT + } + ); + chatClient2.connect(user2.userid, user2.token, () => { + chatClient2.onMessageAdded((message, topic) => { + // console.log("Received:", message.text + ' ID:' + message.message_id); + assert(message != null); + assert(message.text != null); + messages_count += 1; + if (messages_count == TOTAL_MESSAGES) { + endtime = Date.now(); + console.log("End:" + endtime); + let totaltime = endtime - starttime; + console.log("TOTAL TIME:" + totaltime); + chatClient2.close(() => { + console.log("...TEST TERMINATED. DISCONNECTING CLIENT2. TOTAL RECEIVED MESSAGES:" + messages_count); + }); + chatClient1.close(() => { + console.log("CLIENT 1 DISCONNECTED."); + }); + } + }); + }); + + chatClient1.connect(user1.userid, user1.token, () => { + console.log("User1 connected..."); + starttime = Date.now(); + console.log("Start:" + starttime); + for (i = 0; i < TOTAL_MESSAGES; i++) { + let message_text = "Message" + i; + chatClient1.sendMessage( + message_text, + 'text', + user2.userid, + user2.fullname, + user1.fullname, + null, + null, + 'direct', + () => { + // console.log("Message sent."); + } + ); + } + }); +} \ No newline at end of file diff --git a/benchmarks/groups.js b/benchmarks/groups.js new file mode 100644 index 0000000..4515710 --- /dev/null +++ b/benchmarks/groups.js @@ -0,0 +1,162 @@ +var assert = require('assert'); +const { uuid } = require('uuidv4'); +const { Chat21Client } = require('../mqttclient/chat21client.js'); + +const user1 = { + userid: 'USER1', + fullname: 'User 1', + firstname: 'User', + lastname: '1', + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2NzI5NDc4ZS1mOWIwLTRiODctYjNhYS03ZjU1OWExNzc5YjIiLCJzdWIiOiJVU0VSMSIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjEuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIxLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjEiLCJjaWQiOiJVU0VSMSIsImF6cCI6IlVTRVIxIiwidXNlcl9pZCI6IlVTRVIxIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjEiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.r-GBXo1fIUtl1QjOkXxcRaenVNQBElRkus3omh9YtjQ' +}; + + const user2 = { + userid: 'USER2', + fullname: 'User 2', + firstname: 'User', + lastname: '2', + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0MmEwMjc2ZC1lODUzLTQ5YjMtOTU4ZS0xODBkMjFjZGZjNWMiLCJzdWIiOiJVU0VSMiIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjIuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIyLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjIiLCJjaWQiOiJVU0VSMiIsImF6cCI6IlVTRVIyIiwidXNlcl9pZCI6IlVTRVIyIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjIiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.Zkbr3e9MfGGDKRdVUyG4330LxeNaKYS0y3upPtS4Wgg' +}; + +const user3 = { + userid: 'USER3', + fullname: 'User 3', + firstname: 'User', + lastname: '3', + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlMmI2Y2RhMi0yNjhmLTQxZDMtYjBjYy1kZWNjN2I0M2UwMjEiLCJzdWIiOiJVU0VSMyIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjMuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIzLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjMiLCJjaWQiOiJVU0VSMyIsImF6cCI6IlVTRVIzIiwidXNlcl9pZCI6IlVTRVIzIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjMiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.-Cio8ITPCQswv_4KnxJrRbm-5RCXMefuT91wWUNZJmU' +}; +const MQTT_ENDPOINT = 'ws://localhost:15675/ws'; +const API_ENDPOINT = 'http://localhost:8004/api' +const APPID = 'tilechat'; + +let TOTAL_SENT_MESSAGES = 100; +let TOTAL_DELIVERED_MESSAGES = TOTAL_SENT_MESSAGES * 3; +let starttime; +let endtime; + +startBenchmark(); + +function startBenchmark() { + console.log("Sending " + TOTAL_SENT_MESSAGES + " messages to 3 users. Delivering a total of " + TOTAL_DELIVERED_MESSAGES + " messages."); + let messages_count = 0; + const group_id = "group-" + uuid(); + const group_name = "test send message group"; + const group_members = {} + group_members[user1.userid] = 1; + group_members[user2.userid] = 1; + group_members[user3.userid] = 1; + let received_messages = {} + const USER1_RECEIVED_KEY = 'user1'; + const USER2_RECEIVED_KEY = 'user2'; + const USER3_RECEIVED_KEY = 'user3'; + const SENT_MESSAGE = "Welcome everybody 2"; + let chatClient1 = new Chat21Client( // group creator + { + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT, + APIendpoint: API_ENDPOINT + } + ); + let chatClient2 = new Chat21Client( // group member, will receive sent message + { + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT + } + ); + let chatClient3 = new Chat21Client( // group member, will receive sent message + { + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT + } + ); + chatClient2.connect(user2.userid, user2.token, () => { + console.log("User2 connected..."); + chatClient2.onMessageAdded((message, topic) => { + console.log("user2 message added:", message.text); + if ( + message && + message.text && + !message.attributes + ) { + messages_count += 1; + console.log("user2: " + messages_count); + } + check_finish(messages_count, chatClient1, chatClient2, chatClient3); + }); + chatClient3.connect(user3.userid, user3.token, () => { + console.log("User3 connected..."); + chatClient3.onMessageAdded((message, topic) => { + console.log("user3 message added:", message.text); + if ( + message && + message.text && + !message.attributes + ) { + messages_count += 1; + console.log("user3: " + messages_count); + } + check_finish(messages_count, chatClient1, chatClient2, chatClient3); + }); + chatClient1.connect(user1.userid, user1.token, () => { + console.log("User1 (group owner) connected..."); + chatClient1.createGroup( + group_name, + group_id, + group_members, + (err, result) => { + assert(err == null); + assert(result != null); + assert(result.success == true); + assert(result.group.name === group_name); + chatClient1.onMessageAdded((message, topic) => { + console.log("user1 message added:", message.text); + if ( + message && + message.text && + !message.attributes + ) { + messages_count += 1; + console.log("user1: " + messages_count); + } + check_finish(messages_count, chatClient1, chatClient2, chatClient3); + }); + starttime = Date.now(); + console.log("Start:" + starttime); + for (i = 0; i < TOTAL_SENT_MESSAGES; i++) { + let message_text = "Message" + i; + chatClient1.sendMessage( + message_text, + 'text', + group_id, + group_name, + user1.fullname, + null, + null, + 'group', + () => { + //console.log("Message sent to group", group_name); + } + ); + } + } + ); + }); + }); + }); +} + +function check_finish(messages_count, chatClient1, chatClient2, chatClient3) { + if (messages_count == TOTAL_DELIVERED_MESSAGES) { + endtime = Date.now(); + console.log("End:" + endtime); + let totaltime = endtime - starttime; + console.log("TOTAL TIME:" + totaltime); + chatClient1.close(() => { + chatClient2.close(() => { + chatClient3.close(() => { + console.log("BENCHMARK END. TOTAL MESSAGES DELIVERED:", messages_count); + }); + }); + }); + } +} \ No newline at end of file diff --git a/chatdb/index.js b/chatdb/index.js index f797ba3..384c825 100644 --- a/chatdb/index.js +++ b/chatdb/index.js @@ -43,7 +43,7 @@ class ChatDB { delete message['_id'] // if present (message is coming from a mongodb query?) it is illegal. It produces: MongoError: E11000 duplicate key error collection: tiledesk-dialogflow-proxy.messages index: _id_ dup key: { : "5ef72c2494e08ffec88a033a" } this.db.collection(this.messages_collection).updateOne({timelineOf: message.timelineOf, message_id: message.message_id}, { $set: message }, { upsert: true }, function(err, doc) { if (err) { - logger.error("db error...", err) + console.error("db error...", err) if (callback) { callback(err, null) } diff --git a/chatservermq.js b/chatservermq.js index 5729a4a..01f4700 100644 --- a/chatservermq.js +++ b/chatservermq.js @@ -21,11 +21,11 @@ if (webhook_enabled == undefined || webhook_enabled === "true" || webhook_enable }else { webhook_enabled = false; } -logger.info("webhook_enabled: " + webhook_enabled); +logger.debug("webhook_enabled: " + webhook_enabled); var webhook_endpoint = process.env.WEBHOOK_ENDPOINT; -logger.info("webhook_endpoint: " + webhook_endpoint); +logger.debug("webhook_endpoint: " + webhook_endpoint); let webhook_events_array = null; @@ -44,6 +44,7 @@ async function start() { observer.setWebHookEnabled(webhook_enabled); observer.setWebHookEndpoint(webhook_endpoint); observer.setWebHookEvents(webhook_events_array); + // observer.setPersistentMessages(true); await startServer({app_id: process.env.APP_ID, exchange: 'amq.topic', rabbitmq_uri:process.env.RABBITMQ_URI, mongo_uri: process.env.MONGODB_URI}); } diff --git a/mqttclient/chat21client.js b/mqttclient/chat21client.js index 84671f9..119f98c 100644 --- a/mqttclient/chat21client.js +++ b/mqttclient/chat21client.js @@ -102,9 +102,9 @@ class Chat21Client { sendMessage(text, type, recipient_id, recipient_fullname, sender_fullname, attributes, metadata, channel_type, callback) { // callback - function (err) - console.log("recipient_id:", recipient_id) + // console.log("recipient_id:", recipient_id) let dest_topic = `apps/${this.appid}/users/${this.user_id}/messages/${recipient_id}/outgoing` - console.log("dest_topic:", dest_topic) + // console.log("dest_topic:", dest_topic) let outgoing_message = { text: text, type: type, @@ -114,8 +114,7 @@ class Chat21Client { metadata: metadata, channel_type: channel_type } - console.log("outgoing_message:", outgoing_message) - // outgoing_message = {...outgoing_message, ...attributes } + // console.log("outgoing_message:", outgoing_message) const payload = JSON.stringify(outgoing_message) this.client.publish(dest_topic, payload, null, (err) => { callback(err, outgoing_message) @@ -434,7 +433,7 @@ class Chat21Client { if (message_json.attributes && message_json.attributes.updateconversation == false) { update_conversation = false } - console.log("update_conversation........", update_conversation); + // console.log("update_conversation........", update_conversation); if (update_conversation && this.onConversationAddedCallbacks) { this.onConversationAddedCallbacks.forEach((callback, handler, map) => { message_json.is_new = true; @@ -587,7 +586,7 @@ class Chat21Client { callback(null, json.result); } catch (err) { - console.log("parsing json ERROR", err); + console.error("parsing json ERROR", err); callback(err, null); } } @@ -609,7 +608,7 @@ class Chat21Client { callback(null, json.result) } catch (err) { - console.log("parsing json ERROR", err) + console.error("parsing json ERROR", err) callback(err, null) } } @@ -636,7 +635,7 @@ class Chat21Client { callback(null, json.result) } catch (err) { - console.log("parsing json messages ERROR", err) + console.error("parsing json messages ERROR", err) callback(err, null) } } @@ -665,7 +664,7 @@ class Chat21Client { callback(null, null, json) } catch (err) { - console.log("parsing json ERROR", err) + console.error("parsing json ERROR", err) callback(err, null) } } @@ -746,6 +745,7 @@ class Chat21Client { qos: 1, retain: true }, + clientId: this.client_id, username: 'JWT', password: jwt, rejectUnauthorized: false @@ -758,7 +758,7 @@ class Chat21Client { () => { console.log("chat client connected...") if (!this.connected) { - console.log("chat client first connection.") + console.log("Chat client first connection.") this.connected = true this.start() callback() @@ -767,22 +767,22 @@ class Chat21Client { ); this.client.on('reconnect', () => { - console.log("chat client reconnect event"); + console.log("Chat client reconnect event"); } ); this.client.on('close', () => { - console.log("chat client close event"); + console.log("Chat client close event"); } ); this.client.on('offline', () => { - console.log("chat client offline event"); + console.log("Chat client offline event"); } ); this.client.on('error', (error) => { - console.log("chat client error event", error); + console.error("Chat client error event", error); } ); } @@ -823,5 +823,4 @@ else { function isBrowser() { browser = this.window ? true : false; - console.log("browser:", browser); } \ No newline at end of file diff --git a/observer.js b/observer.js index 5f808c8..849f202 100644 --- a/observer.js +++ b/observer.js @@ -89,6 +89,7 @@ logger.info("webhook_enabled: " + webhook_enabled); let webhook_endpoint; let webhook_events_array; +let persistent_messages; function getWebhooks() { return webhooks; @@ -122,6 +123,10 @@ function setWebHookEvents(events) { webhook_events_array = events; } +function setPersistentMessages(persist) { + persistent_messages = persist; +} + function start() { return new Promise(function (resolve, reject) { @@ -416,13 +421,13 @@ function process_outgoing(topic, message_string, callback) { // }) } else { - logger.log("message group."); + logger.debug("Group message."); const group_id = recipient_id chatdb.getGroup(group_id, function(err, group) { // REDIS? // logger.debug("group found!", group) if (!group) { // created only to temporary store group-messages in group-timeline - // TODO: 1. create group (on-the-fly), 2. remove this code, 3. continue as ifthe group exists. - logger.debug("group doesn't exist! Sending anyway to group timeline...") + // TODO: 1. create group (on-the-fly), 2. remove this code, 3. continue as if the group exists. + logger.debug("group doesn't exist! Sending anyway to group timeline..."); group = { uid: group_id, transient: true, @@ -504,7 +509,6 @@ function isMessageGroup(message) { // Places te message in the inbox of the recipient function deliverMessage(message, app_id, inbox_of, convers_with_id, callback) { - logger.log("DELIVERINGMESSAGE:",message) logger.debug("DELIVERING:", message, "inbox_of:", inbox_of, "convers_with:", convers_with_id) // internal flow const persist_topic = `apps.observer.${app_id}.users.${inbox_of}.messages.${convers_with_id}.persist` @@ -524,7 +528,7 @@ function deliverMessage(message, app_id, inbox_of, convers_with_id, callback) { } logger.debug("NOTIFY VIA WHnotifyMessageStatusDelivered, topic: " + added_topic); if (webhooks && webhook_enabled) { - logger.log("webhooks && webhook_enabled ON, processing webhooks, message:", message); + logger.debug("webhooks && webhook_enabled ON, processing webhooks, message:", message); webhooks.WHnotifyMessageStatusSentOrDelivered(message_payload, added_topic, (err) => { if (err) { logger.error("WHnotifyMessageStatusSentOrDelivered with err:"+ err); @@ -664,7 +668,7 @@ function process_persist(topic, message_string, callback) { else { callback(true) } - }) + }); } else { logger.debug("Skip updating conversation. (update_conversation = false)") diff --git a/package-lock.json b/package-lock.json index c5f1a40..7f1c638 100644 --- a/package-lock.json +++ b/package-lock.json @@ -305,6 +305,15 @@ "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.4.tgz", "integrity": "sha512-S/yKGU1syOMzO86+dGpg2qGoDL0zvzcb262G+gqEy6TgP6rt6z6qxSFX/8X6vLC91P7G7C3nLs0+bvDzmvBA3Q==" }, + "buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "requires": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", @@ -507,14 +516,26 @@ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "concat-stream": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", - "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-2.0.0.tgz", + "integrity": "sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==", "requires": { "buffer-from": "^1.0.0", "inherits": "^2.0.3", - "readable-stream": "^2.2.2", + "readable-stream": "^3.0.2", "typedarray": "^0.0.6" + }, + "dependencies": { + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + } } }, "content-disposition": { @@ -552,15 +573,6 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, - "d": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz", - "integrity": "sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==", - "requires": { - "es5-ext": "^0.10.50", - "type": "^1.0.1" - } - }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", @@ -638,71 +650,6 @@ "once": "^1.4.0" } }, - "es5-ext": { - "version": "0.10.53", - "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.53.tgz", - "integrity": "sha512-Xs2Stw6NiNHWypzRTY1MtaG/uJlwCk8kH81920ma8mvN8Xq1gsfhZvpkImLQArw8AHnv8MT2I45J3c0R8slE+Q==", - "requires": { - "es6-iterator": "~2.0.3", - "es6-symbol": "~3.1.3", - "next-tick": "~1.0.0" - } - }, - "es6-iterator": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", - "integrity": "sha1-p96IkUGgWpSwhUQDstCg+/qY87c=", - "requires": { - "d": "1", - "es5-ext": "^0.10.35", - "es6-symbol": "^3.1.1" - } - }, - "es6-map": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/es6-map/-/es6-map-0.1.5.tgz", - "integrity": "sha1-kTbgUD3MBqMBaQ8LsU/042TpSfA=", - "requires": { - "d": "1", - "es5-ext": "~0.10.14", - "es6-iterator": "~2.0.1", - "es6-set": "~0.1.5", - "es6-symbol": "~3.1.1", - "event-emitter": "~0.3.5" - } - }, - "es6-set": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/es6-set/-/es6-set-0.1.5.tgz", - "integrity": "sha1-0rPsXU2ADO2BjbU40ol02wpzzLE=", - "requires": { - "d": "1", - "es5-ext": "~0.10.14", - "es6-iterator": "~2.0.1", - "es6-symbol": "3.1.1", - "event-emitter": "~0.3.5" - }, - "dependencies": { - "es6-symbol": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.1.tgz", - "integrity": "sha1-vwDvT9q2uhtG7Le2KbTH7VcVzHc=", - "requires": { - "d": "1", - "es5-ext": "~0.10.14" - } - } - } - }, - "es6-symbol": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.3.tgz", - "integrity": "sha512-NJ6Yn3FuDinBaBRWl/q5X/s4koRHBrgKAu+yGI6JCBeiu3qrcbJhwT2GeR/EXVfylRk8dpQVJoLEFhK+Mu31NA==", - "requires": { - "d": "^1.0.1", - "ext": "^1.1.2" - } - }, "escalade": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", @@ -723,15 +670,6 @@ "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" }, - "event-emitter": { - "version": "0.3.5", - "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", - "integrity": "sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=", - "requires": { - "d": "1", - "es5-ext": "~0.10.14" - } - }, "express": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", @@ -789,21 +727,6 @@ } } }, - "ext": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/ext/-/ext-1.4.0.tgz", - "integrity": "sha512-Key5NIsUxdqKg3vIsdw9dSuXpPCQ297y6wBjL30edxwPgt2E44WcWBZey/ZvUc6sERLTxKdyCu4gZFmUbk1Q7A==", - "requires": { - "type": "^2.0.0" - }, - "dependencies": { - "type": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/type/-/type-2.5.0.tgz", - "integrity": "sha512-180WMDQaIMm3+7hGXWf12GtdniDEy7nYcyFMKJn/eZz/6tSLXrUN9V0wKSbMjej0I1WHWbpREDEKHtqPQa9NNw==" - } - } - }, "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -1045,6 +968,11 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==" + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -1410,25 +1338,60 @@ } }, "mqtt": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/mqtt/-/mqtt-3.0.0.tgz", - "integrity": "sha512-0nKV6MAc1ibKZwaZQUTb3iIdT4NVpj541BsYrqrGBcQdQ7Jd0MnZD1/6/nj1UFdGTboK9ZEUXvkCu2nPCugHFA==", + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/mqtt/-/mqtt-4.2.6.tgz", + "integrity": "sha512-GpxVObyOzL0CGPBqo6B04GinN8JLk12NRYAIkYvARd9ZCoJKevvOyCaWK6bdK/kFSDj3LPDnCsJbezzNlsi87Q==", "requires": { - "base64-js": "^1.3.0", "commist": "^1.0.0", - "concat-stream": "^1.6.2", - "end-of-stream": "^1.4.1", - "es6-map": "^0.1.5", + "concat-stream": "^2.0.0", + "debug": "^4.1.1", "help-me": "^1.0.1", "inherits": "^2.0.3", - "minimist": "^1.2.0", - "mqtt-packet": "^6.0.0", + "minimist": "^1.2.5", + "mqtt-packet": "^6.6.0", "pump": "^3.0.0", - "readable-stream": "^2.3.6", + "readable-stream": "^3.6.0", "reinterval": "^1.1.0", "split2": "^3.1.0", - "websocket-stream": "^5.1.2", - "xtend": "^4.0.1" + "ws": "^7.3.1", + "xtend": "^4.0.2" + }, + "dependencies": { + "bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "requires": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, + "mqtt-packet": { + "version": "6.10.0", + "resolved": "https://registry.npmjs.org/mqtt-packet/-/mqtt-packet-6.10.0.tgz", + "integrity": "sha512-ja8+mFKIHdB1Tpl6vac+sktqy3gA8t9Mduom1BA75cI+R9AHnZOiaBQwpGiWnaVJLDGRdNhQmFaAqd7tkKSMGA==", + "requires": { + "bl": "^4.0.2", + "debug": "^4.1.1", + "process-nextick-args": "^2.0.1" + } + }, + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + }, + "ws": { + "version": "7.5.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.0.tgz", + "integrity": "sha512-6ezXvzOZupqKj4jUqbQ9tXuJNo+BR2gU8fFRk3XCP3e0G6WT414u5ELe6Y0vtp7kmSJ3F7YWObSNr1ESsgi4vw==" + } } }, "mqtt-packet": { @@ -1495,11 +1458,6 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, - "next-tick": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", - "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=" - }, "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -2032,11 +1990,6 @@ "resolved": "https://registry.npmjs.org/triple-beam/-/triple-beam-1.3.0.tgz", "integrity": "sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==" }, - "type": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz", - "integrity": "sha512-+5nt5AAniqsCnu2cEQQdpzCAh33kVx8n0VoFidKpB1dVVLAN/F+bgVOqOJqOnEnrhp222clB5p3vUlD+1QAnfg==" - }, "type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", diff --git a/package.json b/package.json index 1fdde45..bb1efb6 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "mocha": "^9.0.0", "mongodb": "^3.5.6", "mongoose": "^5.9.10", - "mqtt": "^3.0.0", + "mqtt": "^4.2.6", "rhea": "^1.0.20", "uuidv4": "^6.1.1", "websocket-stream": "^5.5.2", @@ -22,7 +22,8 @@ "axios": "^0.21.1" }, "scripts": { - "test": "mocha ./test/*.js", + "test": "mocha ./test/*.js --timeout 5000", + "benchmarks": "node ./benchmarks/index.js", "start": "node chatservermq.js" }, "author": "", diff --git a/test/test.js b/test/test.js index 85ca6a8..952598f 100644 --- a/test/test.js +++ b/test/test.js @@ -26,15 +26,23 @@ const user3 = { token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlMmI2Y2RhMi0yNjhmLTQxZDMtYjBjYy1kZWNjN2I0M2UwMjEiLCJzdWIiOiJVU0VSMyIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjMuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIzLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjMiLCJjaWQiOiJVU0VSMyIsImF6cCI6IlVTRVIzIiwidXNlcl9pZCI6IlVTRVIzIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjMiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.-Cio8ITPCQswv_4KnxJrRbm-5RCXMefuT91wWUNZJmU' }; +// LOCAL +const MQTT_ENDPOINT = 'ws://localhost:15675/ws'; +const API_ENDPOINT = 'http://localhost:8004/api' +// REMOTE +// const MQTT_ENDPOINT = 'mqtt://99.80.197.164:15675/ws'; +// const API_ENDPOINT = 'http://99.80.197.164:8004/api'; + +const APPID = 'tilechat'; describe('TiledeskClient - test 1', function() { describe('User connects', function() { it('User connects to the RabbbitMQ server through MQTT client', function(done) { let chatClient = new Chat21Client( { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT } ); chatClient.connect(user1.userid, user1.token, () => { @@ -43,7 +51,6 @@ describe('TiledeskClient - test 1', function() { console.log("...and successfully disconnected."); done(); }) - }); }); }); @@ -54,8 +61,8 @@ describe('TiledeskClient - test 2', function() { it('User sends a direct message using client.sendMessage()', function(done) { let chatClient = new Chat21Client( { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT } ); chatClient.connect(user1.userid, user1.token, () => { @@ -87,14 +94,14 @@ describe('TiledeskClient - test 3', function() { it('User1 sends a direct message and User2 receives the message', function(done) { let chatClient1 = new Chat21Client( { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT } ); let chatClient2 = new Chat21Client( { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT } ); chatClient2.connect(user2.userid, user2.token, () => { @@ -115,13 +122,13 @@ describe('TiledeskClient - test 3', function() { console.log("User1 connected..."); chatClient1.sendMessage( 'test', - 'direct', + 'text', user2.userid, user2.fullname, user1.fullname, null, null, - 'chat21', + 'direct', () => { console.log("Message sent."); chatClient1.close(() => { @@ -143,9 +150,9 @@ describe('TiledeskClient - test 4', function() { group_members[user2.userid] = 1; let chatClient = new Chat21Client( { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws', - APIendpoint: 'http://localhost:8004/api' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT, + APIendpoint: API_ENDPOINT } ); chatClient.connect(user1.userid, user1.token, () => { @@ -175,22 +182,22 @@ describe('TiledeskClient - test 4', function() { describe('TiledeskClient - test 5', function() { describe('Creates group with 2 members', function() { - it('Creates a group with 2 members, the added member receives the MEMBER_JOINED_GROUP message', function(done) { + it('Creates a group with 2 members, each added member receives the MEMBER_JOINED_GROUP message', function(done) { const group_id = "group-" + uuid(); const group_name = "test group"; const group_members = {} group_members[user2.userid] = 1; let chatClient1 = new Chat21Client( // group creator { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws', - APIendpoint: 'http://localhost:8004/api' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT, + APIendpoint: API_ENDPOINT } ); let chatClient2 = new Chat21Client( // group member, will receive added-to-group message { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT } ); chatClient2.connect(user2.userid, user2.token, () => { @@ -279,9 +286,9 @@ describe('TiledeskClient - test 6', function() { const SENT_MESSAGE = 'Hello guys'; let chatClient1 = new Chat21Client( // group creator { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws', - APIendpoint: 'http://localhost:8004/api' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT, + APIendpoint: API_ENDPOINT } ); chatClient1.connect(user1.userid, user1.token, () => { @@ -345,21 +352,21 @@ describe('TiledeskClient - test 6', function() { const SENT_MESSAGE = "Welcome everybody 2"; let chatClient1 = new Chat21Client( // group creator { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws', - APIendpoint: 'http://localhost:8004/api' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT, + APIendpoint: API_ENDPOINT } ); let chatClient2 = new Chat21Client( // group member, will receive sent message { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT } ); let chatClient3 = new Chat21Client( // group member, will receive sent message { - appId: "tilechat", - MQTTendpoint: 'ws://localhost:15675/ws' + appId: APPID, + MQTTendpoint: MQTT_ENDPOINT } ); chatClient2.connect(user2.userid, user2.token, () => { diff --git a/webhooks/index.js b/webhooks/index.js index acabf9b..662c546 100644 --- a/webhooks/index.js +++ b/webhooks/index.js @@ -508,7 +508,6 @@ class Webhooks { WHsendData(json, callback) { var q = url.parse(this.webhook_endpoint, true); - logger.debug("ENV WEBHOOK URL PARSED:", q) var protocol = (q.protocol == "http:") ? require('http') : require('https'); let options = { path: q.pathname, @@ -532,7 +531,7 @@ class Webhooks { respdata += chunk; }); response.on('end', () => { - logger.info("WEBHOOK RESPONSE: "+ respdata + " for webhook_endpoint: " + this.webhook_endpoint); + logger.debug("WEBHOOK RESPONSE: "+ respdata + " for webhook_endpoint: " + this.webhook_endpoint); return callback(null, respdata) //TODO SE IL WEBHOOK NN RITORNA SEMBRA CHE SI BLOCCI }); });