I am trying to integrate Socket.io with Angular and I'm having difficulties making a connection from the client-side to the server. I've looked through other related questions but my issue is happening locally, so there's no web server in the middle.
This is what my server code looks like:
const app = express();
const server = http.createServer(app);
const io = require('socket.io').listen(server);
io.on('connection', function(socket) {
socket.emit('greet', { hello: 'Hey, Mr.Client!' });
socket.on('respond', function(data) {
console.log(data);
});
socket.on('disconnect', function() {
console.log('Socket disconnected');
});
});
I'm loading the client side JavaScript files using Grunt in the following order:
dist: {
src: [
public/bower_components/angular/angular.min.js,
...
public/bower_components/socket.io-client/dist/socket.io.min.js,
public/bower_components/angular-socket-io/socket.min.js,
...
]
}
Then in my controller:
function MyController($scope) {
let socket = io.connect(window.location.href);
socket.connect('http://localhost:3000');
socket.on('greet', function(data) {
console.log(data);
socket.emit('respond', { message: 'Hello to you too, Mr.Server!' });
});
...
}
Before actually using the btford/angular-socket-io
library, I want to make sure that I can get a connection correctly, but I get the following error in the console:
The interesting thing is that if I restart the Node.js server process, it does manage to send the message but using polling instead of websockets.
I tried all sorts of different options in the socket.connect call, but nothing worked.
Any help would be appreciated.
UPDATE (30/12/2016):
I just realized that websockets is working partially. I see a 101 Switching Protocols request in the Chrome developer console. However the only frames I see there are the engine.io protocol packets (ping, pong). However my application socket messages still fall back to polling for some reason...
Judging from the messages you send via Socket.IO
socket.emit('greet', { hello: 'Hey, Mr.Client!' });
, it seems that you are using thehackathon-starter
boilerplate. If so, the issue might be thatexpress-status-monitor
module is creating its own socket.io instance, as per: https://github.com/RafalWilinski/express-status-monitor#using-module-with-socketio-in-projectYou can either:
Pass in your socket.io instance and port as
websocket
when you create theexpressStatusMonitor
instance like below:Problem solved! I just figured out how to solve the issue, but I would still like to know if this is normal behavior or not.
It seems that even though the Websocket connection establishes correctly (indicated by the 101 Switching Protocols request), it still defaults to long-polling. The fix was as simple as adding this option to the Socket.io connection function:
So the code finally looks like this:
and on the client:
And the messages now appear as frames:
working websockets
This Github issue pointed me in the right direction. Thanks to everyone who helped out!
I had faced same issues, I refined apache2 virtual host entery and got success.
Note: on server I had succesful installed and working on 9001 port without any issue. This guide line for apache2 only no relavence with nginx, this answer for apache2+etherpad lovers.
Advance tips: Please with help of a2enmod enable all mod of apache2
Restart apache2 than will get effect. But obvious a2ensite to enable site required.
In your controller, you are using an
http
scheme, but I think you should be using aws
scheme, as you are using websockets. Try to usews://localhost:3000
in your connect function.I think you should define your
origins
for client side as bellow:This worked for me with Nginx, Node server and Angular 4
Edit your nginx web server config file as: