logeyg
logeyg

Reputation: 559

Node app; Call server function from Client

I can't seem to call a server function from my client using either socket.io or an ajax call. Any help would be appreciated. For socket.io, I was trying something like this:

server (no error is being thrown, only I never see the console.log):

var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var socket = io.sockets.on('connection', function(socket) {});
socket.on('admin-refresh', function() {
      console.log("*** Admin refresh ***");
    });

client (yes I'm including all necessary files, no error is being thrown on client side nor server):

$('document').ready(function() {

    var socket = io();
    $('#refresh').click(function() {
        console.log('refresh clicked..');
        io.emit('admin-refresh');
    });

});

I don't need any data to be passed, I just want to alert the server to call a function. So perhaps an ajax call would be easier? How would I set up the server to listen for calls?

Upvotes: 0

Views: 125

Answers (1)

DerM
DerM

Reputation: 1526

Why do you have an empty connection callback..? Look at the docs again.

var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);

io.on('connection', function(socket) {
    socket.on('admin-refresh', function() {
      console.log("*** Admin refresh ***");
    });
});

Upvotes: 1

Related Questions