I built a notification system on my website. I've some kind of groups of members. When a member of a group posts something, the other members of the same group should be notified. Notifications appear when the user refreshes the page. However, the world is going in real-time. Googleing the issue recommended me Node.js (Not sure is the best way). I can now: create a server, connect to a database, querying the database, broad messages using socket.io to all connected clients...
But what I can't do/understand so far is to specify to which user(s) the message(notification) should be delivered.
Looking around SO comes with this way that looks what I want:
var socket = io.listen(server);
socket.on('connection', function(client) {
client.on('message', function(msg) {
broadcast(msg); // broadcast message to all clients
// OR
socket.clients[session_id].send(msg); // send message to one client
});
client.on('disconnect', function( ) {
console.log('Client Disconnected.');
});
});
I don't know what session_id is, is it the same as the session_id of the user created when he logs-in? Really got tired looking for some straight forward solutions, any help out will be strongly appreciated.
Here's an example from socket.io docs:
Storing data associated to a client
Sometimes it's necessary to store data associated with a client that's necessary for the duration of the session.
Server side
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('set nickname', function (name) {
socket.set('nickname', name, function () { socket.emit('ready'); });
});
socket.on('msg', function () {
socket.get('nickname', function (err, name) {
console.log('Chat message by ', name);
});
});
});
Client side
<script>
var socket = io.connect('http://localhost');
socket.on('connect', function () {
socket.emit('set nickname', prompt('What is your nickname?'));
socket.on('ready', function () {
console.log('Connected !');
socket.emit('msg', prompt('What is your message?'));
});
});
</script>
Create rooms, multiple clients can be a part of multiple rooms, and clients in a room can broadcast to all other members of the room.
Assuming your message has a room name in it and a data body
socket.on('connection', function(client) {
client.on('message', function(msg) {
// broadcast message to all clients
io.sockets.in(msg.room).emit(msg.data);
// OR
socket.clients[session_id].send(msg); // send message to one client
});
client.on('disconnect', function( ) {
console.log('Client Disconnected.');
});
});
https://github.com/LearnBoost/socket.io This should have some examples
http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/ This is a good one
Socket.IO is super easy to use.
Look at the doc here: https://github.com/learnboost/socket.io/
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.join('justin bieber fans');
socket.broadcast.to('justin bieber fans').emit('new fan');
io.sockets.in('rammstein fans').emit('new non-fan');
});