I have made a LiveQuery and I don't understand why it doesn't responses when I insert something to the Class Buyers.
Here is my code:
function hearingQueries() {
server.liveQuery("LIVE SELECT FROM Buyers")
.on('live-insert', function(data) {
io.emit('chat message', "Server says: " + data.content.message);
})
.on('live-delete', function(data) {
io.emit('chat message', "Server deleted: " + data.content.message);
})
}
The function hearingQueries()
get call when someone connected to the page:
io.on('connection', function(socket) {
console.log('User connected.');
server.open();
hearingQueries();
console.log('Connection succeeded.');
server.open();
is a function that initiate the connection.
My questions are:
Why this doesn't work?
Is it better to start the server when someone connect or when the server goes online?
If it's better to start it when the server starts it doesn't slow the server?
Here is my code so you can see exactly what is going on:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var OrientDB = require('orientjs').ODatabase;
/*if (typeof localStorage === "undefined" || localStorage === null) {
var LocalStorage = require("node-localstorage").LocalStorage;
localStorage = new LocalStorage('./scratch');
}*/
app.get('/indexStyle.css', function(req, res) {
res.sendFile(__dirname + "/indexStyle.css");
});
app.get('/chat', function(req, res) {
res.sendFile(__dirname + "/index.html");
});
app.get('/login.css', function(req, res) {
res.sendFile(__dirname + "/login.css");
});
app.get('/', function(req, res){
res.sendFile(__dirname + "/login.html");
console.log('User has reached the login page.');
});
var msgs = [];
var names = [];
var i = 0;
var server = new OrientDB({
host: 'localhost',
port: 8081,
username: 'root',
password: 'rootPassword',
name: 'DemoUdemy',
useToken: true
});
function openServer(){
server.open().then(function(){
console.log("Server Opened.");
});
}
function closeServer(){
server.close().then(function(){
console.log("Server Closed.");
});
}
function insert(className, name, bucket, color){
server.query("INSERT INTO "+className+"(name, bucket, color) VALUES('"+name+"', '"+bucket+"', '"+color+"')");
}
/*function select(items ,className){
var msgTemp = server.query("SELECT '"+items+"' FROM '"+className+"'");
console.log(msgTemp.Content.Data);
}*/
function hearingQueries(){
server.liveQuery("LIVE SELECT FROM Buyers")
.on('live-insert', function(data){
io.emit('chat message', "Server says: " + data.content.message);
})
.on('live-delete', function(data){
io.emit('chat message', "Server deleted: " + data.content.message);
})
}
io.on('connection', function(socket){
console.log('User connected.');
openServer();
//select("name, bucket", "Buyers");
hearingQueries();
console.log('Connection Succed.');
var iTemp = i - 1;
for(var msg in msgs)
socket.emit('chat message', msgs[msg]);
socket.on('chat message', function(msg){
msgs.push(names[iTemp] + msg);
io.emit('chat message', names[iTemp] + msg);
});
socket.on('getUser', function(user){
i++;
names[i-1] = user + ": ";
});
socket.on('disconnect', function(){
i--;
closeServer();
console.log("User has disconnected.");
});
});
http.listen(8080, function(){
console.log('Listening to Port *8080');
});