Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io configuration

I am trying to set the configuration for socket.io as per https://github.com/LearnBoost/socket.io/wiki/Configuring-Socket.IO

io.set('log level', 1);

But I'm getting TypeError: Object # has no method 'set' ... what's wrong? I have io = require("socket.io")

Also tried,

io.configure('production', function(){
    io.set('log level', 1);
});

but no luck. what's wrong ?

EDIT:

require.paths.push('/cygdrive/c/Personal/software/nodejs/NODE/node_modules');
var express = require("express"),
    fs = require("fs"),
    form = require('connect-form'),
    app = express.createServer(
        form({ keepExtensions: true })
    ),
    sys = require("sys"),
    RentModel = require("./rent_schema"),
    UserModel   = require("./track_schema"),
    io = require("socket.io"),
    fb = require('facebook-js'),
    Twitter = require('./Twitter_Analysis'),
    Foursquare = require('./Foursquare_Analysis'),
    YQL = require("yql"),
    settings = require("./settings");



socket = io.listen(app);
:::::::::::::::::
app.listen(9999);

This works fine.. But if I change it to io = require("socket.io").listen(8080) it gives me error, listen method not found.

like image 805
user644745 Avatar asked Oct 10 '11 08:10

user644745


People also ask

How does exactly Socket.IO work?

The bidirectional channel between the Socket.IO server (Node. js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

How many rooms can Socket.IO handle?

socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.

Does Socket.IO need HTTP?

Even when websockets can be used, the initial connection setup it done over HTTP. Also, a socket.io server will attach to an HTTP server so it can serve its own client code through /socket.io/socket.io.js .


3 Answers

require('socket.io') returns a Socket object. The set properties are on socket.io-manager. The manager is returned by require('socket.io').listen(server) call.

Note that you should be passing a web server to socket.io, not just a port:

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

io.set('log level', 2);

app.listen(8080); // this is the server, not socket.io
like image 154
Kato Avatar answered Sep 26 '22 18:09

Kato


You want var io = require('socket.io').listen(80); instead of just require.

like image 43
smathy Avatar answered Sep 25 '22 18:09

smathy


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

io.configure( function(){
    io.set('log level', 3);

});

There is one important thing to note here. If you get the error*process.nextTick error, or 'error' event on first tick...* that means that you have another webserver, like Apache, listening to port 80.

So, if you chnage the port to, for example 8080, it should work:

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

io.configure( function(){
    io.set('log level', 3);

});

Hope this helps.

like image 44
oscarm Avatar answered Sep 24 '22 18:09

oscarm