In my application, the client first calls a webservice from the nodejs server to get historical data from a mongoDB database in order to plot it (with the highcharts library). Then i need the get the live updates from the server and send them to the client, in order to do so, i want to use socket.io .
I'm using an AJAX function to call a webservice, and after its completion, i'm trying to create a socket.io connection. But this is not working.
Here is a part of my client side code :
$(function(){
$.ajax({
url: 'http://localhost:3300/historicalData',
type: 'GET',
success : function(data) {
//some code here
},
complete : function(){
var socket = io('http://localhost:3300');
//after the connexion is established, i want to do some stuff here
}
});
});
Here is a part of my server side code :
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get("/historicalData", function(req, res){
getData(res);
});
io.on('connection', function(socket){
console.log('A new WebSocket connection has been established');
});
app.listen("3300", function(){
console.log('Server up: http://localhost:3300');
});
What am I doing wrong here?
And what is the correct way to use socket.io within my application ?