Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using socket.io with nodejs and angular

myApp.factory('socket', function ($rootScope) {
  var socket = io.connect('http://localhost');

'io' not defined error

In express i'm writing:-

var io = require('socket.io').listen(app.listen(app.get('port')));
io.sockets.on('connection', function (socket) {
    console.log('io.socket connection');
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

The server is not showing error.

I am trying to follow this tutorial.

Edit:- when i add <script src="http://cdn.socket.io/stable/socket.io.js"></script>

it says connect is not a method of object 'io'

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Socket.io is meant to be installed both on the server and on the client. By calling require() on the server you use the server-side library, but you still need to include the client-side library in the browser.

Put this somewhere in your HTML file (CDN provided by Cloudflare):

<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>

Note this is detailed in the basic example on the front page of the Socket.io website.

share|improve this answer
 
thanks. now it says 'connect' is not method defined. –  Sangram Singh Nov 21 at 8:58
 
Looks like you're using an old version of Socket.io (0.6). See my updated answer, it uses the latest stable version (0.9.16). –  Paul Mougel Nov 21 at 9:06
 
perfect. Thanks! –  Sangram Singh Nov 21 at 9:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.