Gelso77
Gelso77

Reputation: 1905

socket issues with angularJS and NodeJs

I need an help: I am trying to send an emit socket event to the server side. Could you please help me to find out the error ? Please consider the enviroment : client files and server files are running on the same server.

AngularJS:

<!-- Socket.io //bower_components/angular-socket-io/mock/socket-io.js-->                
var socket = io.connect("http://127.0.0.1:3000");      
socket.emit("test","{'xxx': 'yyy'}");

NodeJs:

//"socket.io": "^1.7.4",
var io = require('socket.io').listen(3000);
io.sockets.on('connection', function (socket) {    
      socket.on('test', function (data) {
            console.log("receiving data: " , data);   
      });
)}
running with : nodemon push.js 3000

Thanks in advance. Andrea

Upvotes: 1

Views: 164

Answers (1)

edkeveked
edkeveked

Reputation: 18401

You have a typo in your code: socket instead of sockets.Besides that you're not initializing io as explained in the documentation

var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function (socket) {    
      socket.on('test', function (data) {
            console.log("receiving data: " , data);   
      });
)}

server.listen(3000)

Upvotes: 1

Related Questions