1

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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but it doesn't work either io.socket.on('connection', function (socket) { ^ TypeError: Cannot read property 'on' of undefined
@Gelso77, I edited the answer. it is io.on instead of io.socket.on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.