I'm trying to create a chat module using socket.io. Ideally I would like to pass the port number like this:
var anotherChatApp = require("anotherChatApp")(1337);
the problem I'm having is finding the object after I've set the listen() method.
var io = require("socket.io");
function anotherChatApp(port){
io.listen(port);
}
then I don't know where it goes, because I try to access the io object:
io.sockets.on('connection', function (socket) {
//dosomething
}
and I get runtime errors:
Cannot call method 'on' of undefined
I was loving node until I started building a module.
EDIT:
var io = require("socket.io");
function MacroChat(portNo){
io = require("socket.io").listen(portNo);
}
module.exports = MacroChat;
//here is where the errors come in
io.sockets.on('connection', function (socket){
});
And the error:
TypeError: Cannot read property 'sockets' of undefined
at Object.<anonymous> (D:\wamp\www\nodejitsu\macrochat\node_modules\MacroChat\lib\macrochat.js:10:3)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (D:\wamp\www\nodejitsu\macrochat\node_modules\MacroChat\index.js:5:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
Related
Im following the Getting Started Tutorial on socket.io but when i came to the point of including socket.io itself, im getting an error while running the server in the console.
What am I missing?
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Error:
/usr/share/nginx/html/server/node_modules/socket.io/lib/index.js:177
const keysIterator = this.parentNsps.keys();
^^^^^
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/usr/share/nginx/html/server/index.js:3:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
I was trying to run a MQTT server in nodejs on Ubuntu 14.04 LTS
var mosca = require('mosca')
var settings = {
port: 1883,
persistence: mosca.persistence.Memory
};
var server = new mosca.Server(settings, function() {
console.log('Mosca server is up and running')
});
server.published = function(packet, client, cb) {
if (packet.topic.indexOf('echo') === 0) {
return cb();
}
var newPacket = {
topic: 'echo/' + packet.topic,
payload: packet.payload,
retain: packet.retain,
qos: packet.qos
};
console.log('newPacket', newPacket);
server.publish(newPacket, cb);
}
it is throwing following error:
/home/ubuntu/node_modules/mosca/node_modules/qlobber/lib/qlobber.js:227
for (w of st.keys())
^^ SyntaxError: Unexpected identifier
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/ubuntu/node_modules/mosca/node_modules/qlobber/index.js:3:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Can anyone help what is that I am doing wrong?
of is a keyword in the newer ECMAScript spec, so it appears that the qlobber module used by mosca requires something newer than nodejs 0.10.x
I was trying to run my koa app.js file but I could not get it to work.
Here's my app.js
var koa = require('koa'),
monk = require('monk'),
wrap = require('co-monk');
var db = monk("mongodb url here"),
collection = db.get('mycollection'),
transactions = wrap(collection);
var app = koa();
app.use(function*(){
var res = yield transactions.find({});
console.log(res);
});
app.listen(3000);
and here's the error i got:
TypeError: Cannot read property 'name' of undefined
at makeSkinClass (/home/ric/node_modules/mongoskin/lib/utils.js:33:43)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/grid.js:6:35)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/db.js:22:16)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ric/node_modules/mongoskin/lib/mongo_client.js:5:14)
I'm still new to Koa,nodejs so I'm having a hard time figuring this out. Any help would be great!
I have node.js successfully installed. I have created the file
var http = require('http');
var url=require('url');
var fs=require('fs');
var io = require('socket.io');
http.createServer(function (req, res) {
fs.readFile('/var/www/nodeJS/client.html' ,
function ( err, data ) {
if ( err ) {
console.log( err );
res.writeHead(500);
return res.end( 'Error loading client.html' );
}
res.writeHead( 200 );
res.end( data );
});
}).listen(8124, '127.0.0.1');
io.listen(http);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
The error occurs whenever I use the io object. Without io it works fine.
Error: Cannot find module 'zeparser'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/nodeJS/node_modules/socket.io/node_modules/socket.io-client/node_modules/active-x-obfuscator/index.js:1:78)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
After this I have also separately installed module zeparser. My default npm installation directory seems to be /usr/local/lib/node_modules.
I have set NODE_PATH as
export NODE_PATH="/usr/local/lib/node_modules"
but get the same error.
Then I tried to copy zeparser module to /var/www/nodeJS/node_modules. Then the error changes to
/var/www/nodeJS/node_modules/socket.io/lib/manager.js:104
server.on('error', function(err) {
^
TypeError: Object #<Object> has no method 'on'
at new Manager (/var/www/nodeJS/node_modules/socket.io/lib/manager.js:104:10)
at Object.exports.listen (/var/www/nodeJS/node_modules/socket.io/lib/socket.io.js:78:10)
at Object.<anonymous> (/var/www/nodeJS/app.js:35:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:903:3
On your root project, do:
$ sudo npm install zeparser
It resolved the problem for me.
app.js
var Website = new require( './jslib/website.js' );
./jslib/website.js
var util = require('util'),
events = require('events');
module.exports = Website;
function Website()
{
events.EventEmitter.call( this );
return this;
}
util.inherits( Website, events.EventEmiter );
Console Output
PATH_TO_APPDIR>node app.js
util.js:538
ctor.prototype = Object.create(superCtor.prototype, {
^
TypeError: Cannot read property 'prototype' of undefined
at Object.exports.inherits (util.js:538:43)
at Object.<anonymous> (PATH_TO_APPDIR\jslib\website.js:9:6)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at new require (module.js:378:17)
at Object.<anonymous> (PATH_TO_APPDIR\app.js:1:16)
at Module._compile (module.js:449:26)
This is NodeJS 8.22 on Windows 7*
You have a typo in your code.
util.inherits( Website, events.EventEmiter );
Should be
util.inherits( Website, events.EventEmitter );