I made a simple app with socket.io and node.js, it is a push notification service. When a user connects to the websocket, sends his username (and the name of dashboard) and immediately joins to a room named as the user. The application listens for POST request, saves them and then emits a message to the user's room.
io.on('connection', function(socket) {
socket.on('userid', function(data) {
socket.join(data.userid+'-'+data.dashboard);
notification.find({userid: data.userid, to: data.dashboard, received_by_user: false}, function(err, notifs) {
socket.emit('get_notifications', notifs);
}
}
});
app.post('/notify', function(req, res, next) {
var notif_recv = new notification(req.body);
notif_recv.save(function (err, notif_recv) {
io.sockets.in(notif_recv.userid+'-'+notif_recv.dashboard).emit('new_notification', notif_recv);
});
res.send(200);
});
When I test it with node locally, it works fine, I send a POST
to /notify
and I can see the notification arriving at the dashboard. The problem is, when I test on an AppService on Azure, the client connects to the websocket and receives the first notifications (get_notifications
event), but when I POST
to /notify
, the client doesn't receives anything! (no new_notification
event, io.sockets.in(...).emit(...)
doesnt seems to work on Azure).
For debugging purposes, I used console.log(...) to log at the server the returned value of functions socket.join(...)
and io.sockets.in(...).emit(...)
, resulting that io.socket.in(...).emit(...)
seems to return a io server without any channels and connections!
IIS could be messing with it? I tend to think that IIS have different processes for app.post('/notify'...
and io.on('connection'...
so, the socket I am referencing on app.post is DIFFERENT from I am joining the user in io.on('connection'..
(socket.join(...)
).
Any tip when using socket.io rooms in Azure/IIS? Thanks!