Skip to content

Commit

Permalink
NodeJS helper 1.0v
Browse files Browse the repository at this point in the history
  • Loading branch information
remdex committed Sep 7, 2014
1 parent 8a95aa1 commit b50446e
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
NodeJS-Helper
=============

NodeJS Helper extension
NodeJS Helper extension. Add NodeJS support for Live Helper Chat and reduce server load.

http://livehelperchat.com/node.js-support-extension-to-reduce-server-load-and-use-node.js-as-transport-layer-255a.html
242 changes: 242 additions & 0 deletions nodejshelper/design/nodejshelpertheme/js/customjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
(function($){
var nodejshelper = {

operatorForced : false,

socket : null,

intervalSyncTimeout : null,
chatActivated : false,

init : function(){
this.socket = io.connect(nodejshelperHostConnect,{secure:nodejshelperConfig.secure});

this.socket.on('connect', this.onConnected);
this.socket.on('newmessage', this.onMessage);
this.socket.on('syncforce', this.syncForce);
this.socket.on('usertyping', this.usertyping);
this.socket.on('operatortyping', this.operatortyping);
this.socket.on('userleftchat', this.userleftchat);
this.socket.on('userjoined', this.userjoined);
this.socket.on('addfileupload', this.syncForce);
this.socket.on('addfileuserupload', this.syncForce);
this.socket.on('userpostedmessage', this.userpostedmessage);
this.socket.on('userstartedpostmessage', this.userstartedpostmessage);

// Disable standard sync method
// We will use node JS notifications
clearTimeout(lhinst.userTimeout);

// Required to workflow to work correctly
this.setupForceTimeout();

},

setupForceTimeout : function() {
clearTimeout(this.intervalSyncTimeout);
if (nodejshelperConfig.sync == true && this.chatActivated == false) {
this.intervalSyncTimeout = setTimeout(function(){
nodejshelper.operatorForced = true;
lhinst.syncusercall();
}, nodejshelperConfig.synctimeout*1000);
}
},

syncForce : function(chat_id) {
if (lhinst.chat_id == chat_id) {
nodejshelper.operatorForced = true;
lhinst.syncusercall();
nodejshelper.chatActivated = true;
} else {
lhinst.syncadmincall();
}
},

onMessage : function(messageData) {
if (lhinst.chat_id) {

if (typeof messageData.data.data !== 'undefined') {
if ($('#messagesBlock').find('.usr-tit').size() > 0 && $('#messagesBlock').find('.usr-tit').last().attr('data-sender') == messageData.data.data.sender){
messageData.data.result = messageData.data.data.ur;
} else {
messageData.data.result = messageData.data.data.or;
}

messageData.data.uw = 'false';
messageData.data.blocked = 'false';
messageData.data.status = 'true';
messageData.data.ott = '';
messageData.data.op = '';
messageData.data.error = 'false';
messageData.data.message_id = messageData.data.data.id;

lhinst.updateUserSyncInterface(lhinst,messageData.data);
};
clearTimeout(lhinst.userTimeout);
} else {
lhinst.syncadmincall();
}
},

onConnected : function() {

if (lhinst.chat_id > 0) {
nodejshelper.socket.emit('join',lhinst.chat_id);
};
},

usertyping : function(data) {
if (data.status == false) {
$('#user-is-typing-'+data.chat_id).fadeOut();
} else {
$('#user-is-typing-'+data.chat_id).fadeIn().text(data.msg);
}
},

operatortyping : function(data) {
if (lhinst.chat_id == data.chat_id) {
if (data.status == false) {
setTimeout(function(){
$('#id-operator-typing').fadeOut();
},1000);
} else {
$('#id-operator-typing').fadeIn().text(data.msg);
}
}
},

syncforceaction : function(chat_id) {
nodejshelper.socket.emit('syncforce',chat_id);
},

userleftchat : function(chat_id) {
lhinst.syncadmincall();
},

userjoined : function(chat_id) {
setTimeout(function(){
lhinst.syncadmincall();
},4000);
},

addmsguser : function(inst) {
nodejshelper.socket.emit('userpostedmessage',{chat_id:inst.chat_id});
},

addmsguserbefore : function(inst) {
nodejshelper.socket.emit('userstartedpostmessage',{chat_id:inst.chat_id});
},

userpostedmessage : function() {
lhinst.syncadmincall();
},

userstartedpostmessage : function() {
setTimeout( function() {
lhinst.syncadmincall();
},5000);// Give 5 seconds for user message to be stored in database
},

// Disable user timeout message
syncusercall : function(inst,data) {
clearTimeout(inst.userTimeout);
nodejshelper.setupForceTimeout();
if (nodejshelper.operatorForced == false){
nodejshelper.socket.emit('newmessage',{chat_id:inst.chat_id,data:data});
};
nodejshelper.operatorForced = false;
},

addmsguserchatbox : function(inst,data) {
nodejshelper.operatorForced = true;
nodejshelper.socket.emit('newmessage',{chat_id:inst.chat_id,data:data});
return false;
},

addSynchroChat : function(chat_id,message_id) {
if (nodejshelper.socket) {
nodejshelper.socket.emit('join',chat_id);
} else {
setTimeout(function(){
nodejshelper.socket.emit('join',chat_id);
},1000);
}
},

removeSynchroChat : function(chat_id) {
nodejshelper.socket.emit('leave',chat_id);
},

syncadmincall : function(inst,data) {
clearTimeout(inst.userTimeout);
},

userleftchatNotification : function(chat_id) {
if (nodejshelper.socket) {
nodejshelper.socket.emit('userleftchat',chat_id);
}
},

addFileUserUpload : function(chat_id) {
nodejshelper.socket.emit('syncforce',chat_id);
lhinst.syncusercall();
},

addFileUpload : function(chat_id) {
nodejshelper.socket.emit('syncforce',chat_id);
lhinst.syncadmincall();
},

addRemoteCommand : function(chat_id) {
nodejshelper.socket.emit('syncforce',chat_id);
},

typingStoppedUserInform : function(data) {
nodejshelper.socket.emit('usertyping',data);
},

initTypingMonitoringUserInform : function(data) {
nodejshelper.socket.emit('usertyping',data);
},

initTypingMonitoringAdminInform : function(data) {
data.msg = nodejshelperConfig.typer;
nodejshelper.socket.emit('operatortyping',data);
},

typingStoppedOperatorInform : function(data) {
nodejshelper.socket.emit('operatortyping',data);
}


};

// Give half second for standard script to finish their job
setTimeout(function(){
nodejshelper.init();
},500);

LHCCallbacks.syncadmincall = nodejshelper.syncadmincall;
LHCCallbacks.syncusercall = nodejshelper.syncusercall;
LHCCallbacks.addmsgadmin = nodejshelper.syncforceaction;
LHCCallbacks.addmsguserchatbox = nodejshelper.addmsguserchatbox;
LHCCallbacks.addSynchroChat = nodejshelper.addSynchroChat;
LHCCallbacks.removeSynchroChat = nodejshelper.removeSynchroChat;
LHCCallbacks.initTypingMonitoringAdmin = nodejshelper.initTypingMonitoringAdmin;
LHCCallbacks.userleftchatNotification = nodejshelper.userleftchatNotification;
LHCCallbacks.addFileUserUpload = nodejshelper.addFileUserUpload;
LHCCallbacks.addFileUpload = nodejshelper.addFileUpload;
LHCCallbacks.typingStoppedUserInform = nodejshelper.typingStoppedUserInform;
LHCCallbacks.initTypingMonitoringUserInform = nodejshelper.initTypingMonitoringUserInform;
LHCCallbacks.initTypingMonitoringAdminInform = nodejshelper.initTypingMonitoringAdminInform;
LHCCallbacks.typingStoppedOperatorInform = nodejshelper.typingStoppedOperatorInform;
LHCCallbacks.operatorAcceptedTransfer = nodejshelper.syncforceaction;
LHCCallbacks.uservoted = nodejshelper.syncforceaction;
LHCCallbacks.addRemoteCommand = nodejshelper.addRemoteCommand;
LHCCallbacks.addmsguser = nodejshelper.addmsguser;
LHCCallbacks.addmsguserbefore = nodejshelper.addmsguserbefore;

// Additional options
lhinst.appendSyncArgument = '/(render)/true';

})(jQuery);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
$nodeJsHelperSettings = array (
'prefix' => 'http://',
'host' => 'localhost',
'port' => '31129',
'secure' => false
);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php include_once(erLhcoreClassDesign::designtpl('pagelayouts/parts/nodejshelper_config.tpl.php'));?>
<?php if ( (isset($Result['is_sync_required']) && $Result['is_sync_required'] === true) || erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' ) : ?>
<script>
var nodejshelperHostConnect = '<?php echo $nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>';
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};
<?php if (erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' && erLhcoreClassUser::instance()->isLogged()) :
$currentUser = erLhcoreClassUser::instance();
$userData = $currentUser->getUserData(true); ?>
nodejshelperConfig.typer = '<?php echo htmlspecialchars($userData->name_support,ENT_QUOTES);?> <?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/chat','is typing now...')?>';
<?php endif;?>
</script>
<script src="<?php echo $nodeJsHelperSettings['prefix'],$nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>/socket.io/socket.io.js"></script>
<script type="text/javascript" language="javascript" src="<?php echo erLhcoreClassDesign::designJS('js/customjs.js');?>"></script>
<?php endif; ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php include_once(erLhcoreClassDesign::designtpl('pagelayouts/parts/nodejshelper_config.tpl.php'));?>
<?php if ( (isset($Result['is_sync_required']) && $Result['is_sync_required'] === true) || erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' ) : ?>
<script>
var nodejshelperHostConnect = '<?php echo $nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>';
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};
<?php if (erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' && erLhcoreClassUser::instance()->isLogged()) :
$currentUser = erLhcoreClassUser::instance();
$userData = $currentUser->getUserData(true); ?>
nodejshelperConfig.typer = '<?php echo htmlspecialchars($userData->name_support,ENT_QUOTES);?> <?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/chat','is typing now...')?>';
<?php endif;?>
</script>
<script src="<?php echo $nodeJsHelperSettings['prefix'],$nodeJsHelperSettings['host']?>:<?php echo $nodeJsHelperSettings['port']?>/socket.io/socket.io.js"></script>
<script type="text/javascript" language="javascript" src="<?php echo erLhcoreClassDesign::designJS('js/customjs.js');?>"></script>
<?php endif; ?>
18 changes: 18 additions & 0 deletions nodejshelper/doc/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Author: Remigijus Kiminas
Version: 1.0

This module brings nodejs as transport helper. Note all messages are synced using nodejs but the most frequest does.

Requirements:
a. nodejs on server
b. Browser with WebSockets support.

Install:
1. Put nodejshelper on your server extensions folder.
2. Adjust settings in:
2.1 extension/nodejshelper/server/settings.js file
2.2 extension/nodejshelper/design/nodejshelpertheme/tpl/pagelayouts/parts/page_head_js_extension.tpl.php set your node.js server address and port.
3. Enable extension in settings.ini.php file.
4. Clean cache from back office.
5. run node server.js from extension/nodejshelper/server folder.
6. Start some chat, you should see some messages in console.
Loading

0 comments on commit b50446e

Please sign in to comment.