1
votes

I use node.js with express.js framework. I Tried connect to server using io.connect on client side.

Template:

html
head
    title Example chat
    script(src="http://code.jquery.com/jquery-latest.min.js")
    script(src="http://cdn.socket.io/stable/socket.io.js")
    script(src="javascripts/chat.js")
body
    form
        input(type="text" name="msg")
        input(type="submit")

It's chat.js:

var socket = io.connect('//localhost:8080');

app.js socket.io require:

var io = require('socket.io').listen(8080);

Firebug gives an error:

TypeError: io.connect is not a function

What is it? How to fix it?

Thanks you.

1
Are you sure your server is listening on port 3000? Try also: socket = io.connect('http://localhost:3000/');Morrisda
@Morrisda, TypeError: io.connect is not a function. Tried to change port - same result.owl
that error is about - not receiving the socket.IO file from server. Check url once again, show some more code from server.Pranav
did you handle app->get('/') on server ?Pranav
Sorry, I use it for the first time. Pastebin: pastebin.com/4Y5xAuW5 (It's app.js)owl

1 Answers

0
votes

Server-side

var express = require('express');
var http = require('http');
var io = require('socket.io');
var app = express()
  , server = require('http').createServer(app)
  , io = io.listen(server);

server.listen(app.get('port'), function(){
  console.log("✔ Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Client-side

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

Then if you're wondering where that socket.io.js is found, it's automatically generated.