Reputation: 75
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!
Upvotes: 1
Views: 843
Reputation: 13918
To test the socket.io
and POST
issue on Azure App Services, I simplify the project and test on Azure.
The test server.js
:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
server.listen(process.env.PORT ||3000, function(){
console.log('listening on *:3000');
});
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client.html');
});
app.post('/notify', function(req, res, next) {
io.sockets.emit('new_notification', req.body);
res.send(200);
});
io.on('connection', function (socket) {
socket.emit('get_notifications', "get_notifications_:"+new Date());
});
The client content:
<body>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$.post("/notify",{
msg:"notification from client"
});
socket.on('get_notifications',function(msg){
console.log(msg);
});
socket.on('new_notification',function(msg){
console.log(msg);
});
</script>
</body>
And this simple project works fine on Azure App Services.
So it could be some other part of your application which raise your issue.
You can check the Diagnostics Logs
or leverage Visual Studio Team Services to troubleshooting the issue. Refer to the answer of How to run django manage.py command on Azure App Service for how the enable the Team Services extension on Azure App Services.
Any further concern, please feel free to let me know.
Upvotes: 0
Reputation: 1
Did you try adding a 'disconnect' handler for debugging?
io.on('connection', function (socket) {
socket.on('disconnect', function(){ //add this part
console.log('Client disconnected');
});
socket.emit('get_notifications', "get_notifications_:"+new Date());
});
Also log at every emit and receive, so that you know when the client gets disconnected exactly. If its getting disconnected after the get_notifications
event, make sure you are not sending a callback to the client or that the client is not expecting a callback from the server.
I notice some missing closing braces for the notification.find(..)
and socket.on('userid'..)
. I hope that is only in the code that you pasted here.
Upvotes: 0