Reputation: 1
I have a totally seperate website written in PHP and I'm trying to use the capability of node and socketIO together to send messages.
I want to create a "{username} visited {page}" system so I can see in real time when my users are logging in, what pages their visiting and other things on the site.
I'm not sure if NodeJS + SocketIO is the best thing for this but I didn't know any better way. The problem comes when every example I find on google relys on express.
Is there or can anyone post a clean minimial example of socketIO & code without depending on express to print the html.
Upvotes: 0
Views: 2513
Reputation: 14165
Your concept is solid (PHP for app, SocketIO for dynamic data updates). SocketIO can use the basic http server per below. Don't forget to add the client library for socketIO. Directly from the docs here https://socket.io/docs/:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(80);// <---- change the port
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
And here is the client from the docs:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Upvotes: 1