Making Requests from the Client.
Without Socket.IO
Clientside:
$('button').click(function () { $.post('/thing', {data: 'blah'}, function (data) { console.log(data); }); }, 'json');
Serverside:
var express = require('express'); var bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.urlEncoded()); app.post('/thing', function (req, res, next) { var data = myFunction(req.body); res.json(data); });
$('button').click(function () { $.post('/thing', {data: 'blah'}, function (data) { console.log(data); }); }, 'json');
Serverside:
var express = require('express'); var bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.urlEncoded()); app.post('/thing', function (req, res, next) { var data = myFunction(req.body); res.json(data); });
With Socket.IO
That should work fine, just make sure that in your index.html you have :
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
also, since you're serving your page via Apache, you really don't need the handler and the http server in you node file. this should work just fine :
var io = require('socket.io').listen(8080); io.sockets.on('connection', function (socket) { socket.on('my event', function (msg) { console.log("DATA!!!"); }); });
and for the index.html :
<!DOCTYPE html> <html lang="en"> <head> <title>Hello World!</title> <meta charset="utf-8"> <script src="http://localhost:8080/socket.io/socket.io.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ var socket = io.connect('http://localhost:8080'); $("#button").click(function() { socket.emit('my event' ,"Hello World!"); }) }) </script> </head> <body> <button type="button" id='button'>Send Message</button> </body> </html>
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
also, since you're serving your page via Apache, you really don't need the handler and the http server in you node file. this should work just fine :
var io = require('socket.io').listen(8080); io.sockets.on('connection', function (socket) { socket.on('my event', function (msg) { console.log("DATA!!!"); }); });
and for the index.html :
<!DOCTYPE html> <html lang="en"> <head> <title>Hello World!</title> <meta charset="utf-8"> <script src="http://localhost:8080/socket.io/socket.io.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ var socket = io.connect('http://localhost:8080'); $("#button").click(function() { socket.emit('my event' ,"Hello World!"); }) }) </script> </head> <body> <button type="button" id='button'>Send Message</button> </body> </html>
References:
[1] http://stackoverflow.com/questions/25144531/detect-button-click-with-node-js
[2] http://stackoverflow.com/questions/10071406/how-to-send-messages-using-socket-io
[3] https://www.youtube.com/watch?v=pNKNYLv2BpQ
[2] http://stackoverflow.com/questions/10071406/how-to-send-messages-using-socket-io
[3] https://www.youtube.com/watch?v=pNKNYLv2BpQ