Reputation: 1453
I'm doing a system using expressjs + socket.io(with SessionSocket plugin) + mongoosejs.
The server was build like this:
var mongoose = require('mongoose'),
connect = require('connect'),
express = require('express'),
http = require('http'),
io = require('socket.io');
// hold cookieParser and sessionStore for SessionSocket
var cookieParser = express.cookieParser('your secret sauce'),
sessionStore = new connect.middleware.session.MemoryStore();
// create express server
var app = express();
// config express server
require('./config/express')(app, config, cookieParser, sessionStore);
// Express3 requires a http.Server to attach socke.io
var server = http.createServer(app);
// attach socket.io
var sio = io.listen(server);
sio.set('log level', 2);
// create SessionSocket
var SessionSockets = require('session.socket.io'),
sessionSockets = new SessionSockets(sio, sessionStore, cookieParser);
// setup SessionSocket
sessionSockets.on('connection', function(err, socket, session) {
socket.join(session.user._id);
socket.emit('message', {
title: "welcome",
msg: "welcome to selink"
});
});
// config express server route
app.get('/some-action', **SomeMongooseFunction**);
// start server
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
In the express route handler("SomeMongooseFunction", defined in other file), after processed user action(query, modify DB etc.), I want sent a real-time notification to the user or other online user. so I thing I must pass "sio" object into the "SomeMongooseFunction", the example above has only one route, but actually I have dozens of routes, It gonna looks ugly if I do so.
or in the setup SessionSocket part, I think could paste the sio into the user's session. but, I'm not sure this way is right or not.
so, is there any other way to get sio reference in the express handler? (it will be nice if I can refer sio like a global variable, but global is evil...)
thanks for any ideas.
Upvotes: 1
Views: 842
Reputation: 2167
You're storing user ID
from session into Socket.IO rooms manager. Now you have somehow get the target's user ID
and send the event message for this user.
For your case, you are using notification
entity so lets do it:
//client side - send a notification for a specific user.
var note = {
from: userID, //sender
target: id, //receiver
type: type //type of notification
}
socket.emit('notification', note);
target
field is the target user ID, this user will receive the notification status.
type
here is just for illustrate if you wanna to handle more than one type of notifications.
socket.emit('notification', note);
this part is the most important one. It will send the note JSON object
to notification event. This event must be registered on you server side Socket.IO page;
//server side - receive JSON data on notification event
socket.on('notification', function(data) {
io.sockets.in(data.target).emit('notification', data);
});
NOTE: I use 'io' instead of 'sio'.
notification event is registered and will catch all client messages directed to it. data
is the
JSON object we sent before, so io.sockets.in().emit()
Once again on client. We must catch all notification event.
//client side - the final destination
socket.on('notification', function(data) {
//DO SOME JQUERY STUFF TO ALERT THIS TARGET USER FROM THE MESSAGE RECEIVED.
console.log("User: " + data.from + " sent you this notification");
});
The steps we can conclude are:
You can use this analogy for any case you have to send Socket.IO messages for a specific user.
Upvotes: 1