I am trying to implement socket.io into my application which is hosted at Azurewebsites. webapp
Here is the server.js
var app = require('express')();
var server = require('http').createServer(app);
server.listen(process.env.PORT || 3001)
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log("Socket connected :"+socket.id);
socket.emit('news', { hello: 'world' });
});
And Here is the client side socket. index.html
<script src="socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:3001');
console.log("scoekt connect",socket)
socket.on('connect', function(){ console.log('connected to socket'); });
socket.on('error', function(e){ console.log('error' + e); });
socket.on( 'news', function( data ){
console.log("socket data",data);
});</script>
I am not really sure whats is going wrong. Here is the structure of file-ing system
ROOT
app/
index.html
server.js
web.config
PS: this is an Angular2 application
PS: I have checked all the suggested question based on this error but none solved my issue, thus i am posting this question.
Per my experience, Azure Web App doesn't bind
loaclhost
or127.0.0.1
to your website, and only ports 80 and 443 are public-facing. This maps to a specific port for your app to listen to, retrievable viaprocess.env.PORT
. So you'd need to replacewith
And if your server side and client side in the different domain, you'd also need to enable CORS on the server side. In Azure, we can enable it with the Azure portal.
Socket.IO uses WebSockets, which are not enabled by default on Azure. You can also enable WebSocket support using the Azure Portal. Please see the steps below.
For more info, please refer to this documentation.