Smit Smit - 4 years ago 816
Node.js Question


I am trying to implement into my application which is hosted at Azurewebsites.


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('')(server);

io.on('connection', function (socket) {
console.log("Socket connected :";
socket.emit('news', { hello: 'world' });

And Here is the client side socket. index.html

<script src=""></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);


I am getting the below error
enter image description here

I am not really sure whats is going wrong. Here is the structure of file-ing system


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.

Answer Source

Per my experience, Azure Web App doesn't bind loaclhost or 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 via process.env.PORT. So you'd need to replace

var socket = io('http://localhost:3001');


var socket = io('http://<your app name>');

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.

  • In a browser, go to the Azure portal, and navigate to your App Service.
  • Click CORS in the API menu.
  • Enter each URL in the empty Allowed Origins text box. A new text box is created. As an alternative, you can enter an asterisk (*) to specify that all origin domains are accepted.
  • Click Save.

enter image description here

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.

  • In the Azure portal, click Application settings in the SETTINGS menu.
  • Under Web Sockets click On
  • Click Save.

enter image description here

For more info, please refer to this documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download