Skip to content

Commit b50446e

Browse files
committed
NodeJS helper 1.0v
1 parent 8a95aa1 commit b50446e

File tree

8 files changed

+416
-1
lines changed

8 files changed

+416
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
NodeJS-Helper
22
=============
33

4-
NodeJS Helper extension
4+
NodeJS Helper extension. Add NodeJS support for Live Helper Chat and reduce server load.
5+
6+
http://livehelperchat.com/node.js-support-extension-to-reduce-server-load-and-use-node.js-as-transport-layer-255a.html
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
(function($){
2+
var nodejshelper = {
3+
4+
operatorForced : false,
5+
6+
socket : null,
7+
8+
intervalSyncTimeout : null,
9+
chatActivated : false,
10+
11+
init : function(){
12+
this.socket = io.connect(nodejshelperHostConnect,{secure:nodejshelperConfig.secure});
13+
14+
this.socket.on('connect', this.onConnected);
15+
this.socket.on('newmessage', this.onMessage);
16+
this.socket.on('syncforce', this.syncForce);
17+
this.socket.on('usertyping', this.usertyping);
18+
this.socket.on('operatortyping', this.operatortyping);
19+
this.socket.on('userleftchat', this.userleftchat);
20+
this.socket.on('userjoined', this.userjoined);
21+
this.socket.on('addfileupload', this.syncForce);
22+
this.socket.on('addfileuserupload', this.syncForce);
23+
this.socket.on('userpostedmessage', this.userpostedmessage);
24+
this.socket.on('userstartedpostmessage', this.userstartedpostmessage);
25+
26+
// Disable standard sync method
27+
// We will use node JS notifications
28+
clearTimeout(lhinst.userTimeout);
29+
30+
// Required to workflow to work correctly
31+
this.setupForceTimeout();
32+
33+
},
34+
35+
setupForceTimeout : function() {
36+
clearTimeout(this.intervalSyncTimeout);
37+
if (nodejshelperConfig.sync == true && this.chatActivated == false) {
38+
this.intervalSyncTimeout = setTimeout(function(){
39+
nodejshelper.operatorForced = true;
40+
lhinst.syncusercall();
41+
}, nodejshelperConfig.synctimeout*1000);
42+
}
43+
},
44+
45+
syncForce : function(chat_id) {
46+
if (lhinst.chat_id == chat_id) {
47+
nodejshelper.operatorForced = true;
48+
lhinst.syncusercall();
49+
nodejshelper.chatActivated = true;
50+
} else {
51+
lhinst.syncadmincall();
52+
}
53+
},
54+
55+
onMessage : function(messageData) {
56+
if (lhinst.chat_id) {
57+
58+
if (typeof messageData.data.data !== 'undefined') {
59+
if ($('#messagesBlock').find('.usr-tit').size() > 0 && $('#messagesBlock').find('.usr-tit').last().attr('data-sender') == messageData.data.data.sender){
60+
messageData.data.result = messageData.data.data.ur;
61+
} else {
62+
messageData.data.result = messageData.data.data.or;
63+
}
64+
65+
messageData.data.uw = 'false';
66+
messageData.data.blocked = 'false';
67+
messageData.data.status = 'true';
68+
messageData.data.ott = '';
69+
messageData.data.op = '';
70+
messageData.data.error = 'false';
71+
messageData.data.message_id = messageData.data.data.id;
72+
73+
lhinst.updateUserSyncInterface(lhinst,messageData.data);
74+
};
75+
clearTimeout(lhinst.userTimeout);
76+
} else {
77+
lhinst.syncadmincall();
78+
}
79+
},
80+
81+
onConnected : function() {
82+
83+
if (lhinst.chat_id > 0) {
84+
nodejshelper.socket.emit('join',lhinst.chat_id);
85+
};
86+
},
87+
88+
usertyping : function(data) {
89+
if (data.status == false) {
90+
$('#user-is-typing-'+data.chat_id).fadeOut();
91+
} else {
92+
$('#user-is-typing-'+data.chat_id).fadeIn().text(data.msg);
93+
}
94+
},
95+
96+
operatortyping : function(data) {
97+
if (lhinst.chat_id == data.chat_id) {
98+
if (data.status == false) {
99+
setTimeout(function(){
100+
$('#id-operator-typing').fadeOut();
101+
},1000);
102+
} else {
103+
$('#id-operator-typing').fadeIn().text(data.msg);
104+
}
105+
}
106+
},
107+
108+
syncforceaction : function(chat_id) {
109+
nodejshelper.socket.emit('syncforce',chat_id);
110+
},
111+
112+
userleftchat : function(chat_id) {
113+
lhinst.syncadmincall();
114+
},
115+
116+
userjoined : function(chat_id) {
117+
setTimeout(function(){
118+
lhinst.syncadmincall();
119+
},4000);
120+
},
121+
122+
addmsguser : function(inst) {
123+
nodejshelper.socket.emit('userpostedmessage',{chat_id:inst.chat_id});
124+
},
125+
126+
addmsguserbefore : function(inst) {
127+
nodejshelper.socket.emit('userstartedpostmessage',{chat_id:inst.chat_id});
128+
},
129+
130+
userpostedmessage : function() {
131+
lhinst.syncadmincall();
132+
},
133+
134+
userstartedpostmessage : function() {
135+
setTimeout( function() {
136+
lhinst.syncadmincall();
137+
},5000);// Give 5 seconds for user message to be stored in database
138+
},
139+
140+
// Disable user timeout message
141+
syncusercall : function(inst,data) {
142+
clearTimeout(inst.userTimeout);
143+
nodejshelper.setupForceTimeout();
144+
if (nodejshelper.operatorForced == false){
145+
nodejshelper.socket.emit('newmessage',{chat_id:inst.chat_id,data:data});
146+
};
147+
nodejshelper.operatorForced = false;
148+
},
149+
150+
addmsguserchatbox : function(inst,data) {
151+
nodejshelper.operatorForced = true;
152+
nodejshelper.socket.emit('newmessage',{chat_id:inst.chat_id,data:data});
153+
return false;
154+
},
155+
156+
addSynchroChat : function(chat_id,message_id) {
157+
if (nodejshelper.socket) {
158+
nodejshelper.socket.emit('join',chat_id);
159+
} else {
160+
setTimeout(function(){
161+
nodejshelper.socket.emit('join',chat_id);
162+
},1000);
163+
}
164+
},
165+
166+
removeSynchroChat : function(chat_id) {
167+
nodejshelper.socket.emit('leave',chat_id);
168+
},
169+
170+
syncadmincall : function(inst,data) {
171+
clearTimeout(inst.userTimeout);
172+
},
173+
174+
userleftchatNotification : function(chat_id) {
175+
if (nodejshelper.socket) {
176+
nodejshelper.socket.emit('userleftchat',chat_id);
177+
}
178+
},
179+
180+
addFileUserUpload : function(chat_id) {
181+
nodejshelper.socket.emit('syncforce',chat_id);
182+
lhinst.syncusercall();
183+
},
184+
185+
addFileUpload : function(chat_id) {
186+
nodejshelper.socket.emit('syncforce',chat_id);
187+
lhinst.syncadmincall();
188+
},
189+
190+
addRemoteCommand : function(chat_id) {
191+
nodejshelper.socket.emit('syncforce',chat_id);
192+
},
193+
194+
typingStoppedUserInform : function(data) {
195+
nodejshelper.socket.emit('usertyping',data);
196+
},
197+
198+
initTypingMonitoringUserInform : function(data) {
199+
nodejshelper.socket.emit('usertyping',data);
200+
},
201+
202+
initTypingMonitoringAdminInform : function(data) {
203+
data.msg = nodejshelperConfig.typer;
204+
nodejshelper.socket.emit('operatortyping',data);
205+
},
206+
207+
typingStoppedOperatorInform : function(data) {
208+
nodejshelper.socket.emit('operatortyping',data);
209+
}
210+
211+
212+
};
213+
214+
// Give half second for standard script to finish their job
215+
setTimeout(function(){
216+
nodejshelper.init();
217+
},500);
218+
219+
LHCCallbacks.syncadmincall = nodejshelper.syncadmincall;
220+
LHCCallbacks.syncusercall = nodejshelper.syncusercall;
221+
LHCCallbacks.addmsgadmin = nodejshelper.syncforceaction;
222+
LHCCallbacks.addmsguserchatbox = nodejshelper.addmsguserchatbox;
223+
LHCCallbacks.addSynchroChat = nodejshelper.addSynchroChat;
224+
LHCCallbacks.removeSynchroChat = nodejshelper.removeSynchroChat;
225+
LHCCallbacks.initTypingMonitoringAdmin = nodejshelper.initTypingMonitoringAdmin;
226+
LHCCallbacks.userleftchatNotification = nodejshelper.userleftchatNotification;
227+
LHCCallbacks.addFileUserUpload = nodejshelper.addFileUserUpload;
228+
LHCCallbacks.addFileUpload = nodejshelper.addFileUpload;
229+
LHCCallbacks.typingStoppedUserInform = nodejshelper.typingStoppedUserInform;
230+
LHCCallbacks.initTypingMonitoringUserInform = nodejshelper.initTypingMonitoringUserInform;
231+
LHCCallbacks.initTypingMonitoringAdminInform = nodejshelper.initTypingMonitoringAdminInform;
232+
LHCCallbacks.typingStoppedOperatorInform = nodejshelper.typingStoppedOperatorInform;
233+
LHCCallbacks.operatorAcceptedTransfer = nodejshelper.syncforceaction;
234+
LHCCallbacks.uservoted = nodejshelper.syncforceaction;
235+
LHCCallbacks.addRemoteCommand = nodejshelper.addRemoteCommand;
236+
LHCCallbacks.addmsguser = nodejshelper.addmsguser;
237+
LHCCallbacks.addmsguserbefore = nodejshelper.addmsguserbefore;
238+
239+
// Additional options
240+
lhinst.appendSyncArgument = '/(render)/true';
241+
242+
})(jQuery);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$nodeJsHelperSettings = array (
3+
'prefix' => 'http://',
4+
'host' => 'localhost',
5+
'port' => '31129',
6+
'secure' => false
7+
);
8+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php include_once(erLhcoreClassDesign::designtpl('pagelayouts/parts/nodejshelper_config.tpl.php'));?>
2+
<?php if ( (isset($Result['is_sync_required']) && $Result['is_sync_required'] === true) || erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' ) : ?>
3+
<script>
4+
var nodejshelperHostConnect = '<?php echo $nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>';
5+
var nodejshelperConfig = {secure:<?php echo $nodeJsHelperSettings['secure'] == true ? 'true' : 'false' ?>,'typer':'','sync':<?php echo (isset($Result['chat']) && $Result['chat']->status == erLhcoreClassModelChat::STATUS_PENDING_CHAT) ? 'true' : 'false'?>,'synctimeout':5};
6+
<?php if (erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' && erLhcoreClassUser::instance()->isLogged()) :
7+
$currentUser = erLhcoreClassUser::instance();
8+
$userData = $currentUser->getUserData(true); ?>
9+
nodejshelperConfig.typer = '<?php echo htmlspecialchars($userData->name_support,ENT_QUOTES);?> <?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/chat','is typing now...')?>';
10+
<?php endif;?>
11+
</script>
12+
<script src="<?php echo $nodeJsHelperSettings['prefix'],$nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>/socket.io/socket.io.js"></script>
13+
<script type="text/javascript" language="javascript" src="<?php echo erLhcoreClassDesign::designJS('js/customjs.js');?>"></script>
14+
<?php endif; ?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php include_once(erLhcoreClassDesign::designtpl('pagelayouts/parts/nodejshelper_config.tpl.php'));?>
2+
<?php if ( (isset($Result['is_sync_required']) && $Result['is_sync_required'] === true) || erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' ) : ?>
3+
<script>
4+
var nodejshelperHostConnect = '<?php echo $nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>';
5+
var nodejshelperConfig = {secure:<?php echo $nodeJsHelperSettings['secure'] == true ? 'true' : 'false' ?>,'typer':'','sync':<?php echo (isset($Result['chat']) && $Result['chat']->status == erLhcoreClassModelChat::STATUS_PENDING_CHAT) ? 'true' : 'false'?>,'synctimeout':5};
6+
<?php if (erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' && erLhcoreClassUser::instance()->isLogged()) :
7+
$currentUser = erLhcoreClassUser::instance();
8+
$userData = $currentUser->getUserData(true); ?>
9+
nodejshelperConfig.typer = '<?php echo htmlspecialchars($userData->name_support,ENT_QUOTES);?> <?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/chat','is typing now...')?>';
10+
<?php endif;?>
11+
</script>
12+
<script src="<?php echo $nodeJsHelperSettings['prefix'],$nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>/socket.io/socket.io.js"></script>
13+
<script type="text/javascript" language="javascript" src="<?php echo erLhcoreClassDesign::designJS('js/customjs.js');?>"></script>
14+
<?php endif; ?>

nodejshelper/doc/README

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Author: Remigijus Kiminas
2+
Version: 1.0
3+
4+
This module brings nodejs as transport helper. Note all messages are synced using nodejs but the most frequest does.
5+
6+
Requirements:
7+
a. nodejs on server
8+
b. Browser with WebSockets support.
9+
10+
Install:
11+
1. Put nodejshelper on your server extensions folder.
12+
2. Adjust settings in:
13+
2.1 extension/nodejshelper/server/settings.js file
14+
2.2 extension/nodejshelper/design/nodejshelpertheme/tpl/pagelayouts/parts/page_head_js_extension.tpl.php set your node.js server address and port.
15+
3. Enable extension in settings.ini.php file.
16+
4. Clean cache from back office.
17+
5. run node server.js from extension/nodejshelper/server folder.
18+
6. Start some chat, you should see some messages in console.

0 commit comments

Comments
 (0)