Hello I am currently doing a real time inbox on my project. I got two users the Customer and the Admin, the admin page is subdomained e.g subdomain.website.com, Now when I Emit or send message on my Admin page which is on the subdomain the Customer is not recieving, everything works fine on their own domains. My question is, is it possible for a subdomain to join the same node server that main site uses? Thanks, Below is my code.
Server:
var io = require('socket.io').listen(3010);
var mysql = require('mysql');
const dateTime = require('date-time');
io.on('connection', function(socket){
var con = mysql.createConnection({
host: "127.0.0.1",
user: "devshop",
password: "!",
database: "!"
});
con.connect(function(err) {
if (err) throw err;
});
socket.on('set customer socket',function(data){
socket.join('session_'+data.customer_id); //customer_socket is just a random unique number in this case it can be customer id
con.query("SELECT ci.id,ci.customer_id,ci.message,user.firstname,user.lastname, ci.date_added,ci.date_viewed,ci.type FROM customer_inbox ci LEFT JOIN user ON ci.sender_id = user.user_id WHERE ci.customer_id = "+data.customer_id+" ", function (err, result) {
if (err) throw err;
io.to('session_'+data.customer_id).emit('get customer inbox',result);
});
});
This is the sending message part on server
socket.on('send message customer',function(data){
socket.join('session_'+data.customer_id);
con.query("INSERT INTO customer_inbox (customer_id,sender_id,message,date_added,type) VALUES ("+data.customer_id+",1,'"+data.message+"', '"+dateTime()+"',1) ", function (err, result) {
if (err) throw err;
var resultlast = result.insertId;
con.query("SELECT ci.id,ci.customer_id,ci.message,user.firstname,user.lastname, ci.date_added,ci.date_viewed,ci.type FROM customer_inbox ci LEFT JOIN user ON ci.sender_id = user.user_id WHERE ci.id = "+resultlast+" ", function (err, result) {
if (err) throw err;
io.to('session_'+data.customer_id).emit('append new message',result);
});
});
});
Here is the Client side of admin
$('body').on('click','.btn-send-submit',function(){
socket.emit('send message customer',{customer_id:$(this).data('id'),message:$('#message_content').val()});
});
and this is the client side of Customer
socket.emit('set customer socket',{customer_id : customer_id});
socket.on('get customer inbox',function(data){
if(!customer_node.set){
customer_node.set_inbox(data);
customer_node.set = true;
}
});
socket.on('set message read',function(data){
customer_message.set_inbox_read(data);
});
socket.on('append new message',function(data){
console.log(data);
customer_node.append_message(data);
});
Will it be possible to share one node server js and same port on two doamains? or main domain and subdomain?
via user2990463
No comments:
Post a Comment