Accessing io object in app.js - javascript

I have an application built on express. I have an app.js that starts the app, and a functions.js where I have stored a lot of the functionality of the app. I would like to be able to access the socket.io object to use in my functions.js. However, it seems that I cannot do this and I can't find any solution on the internet either.
app.js:
var app = express(); //var app = express.createServer();
var http = require('http').createServer(app).listen(3001);
var io = require('socket.io')(http);
functions.js:
//something like io = require('./app').io
I want to be able to access the io object so I can emit messages to my client side javascript.

Use module.exports to make the io object available to other modules.
app.js:
var express = require('express');
var app = express();
var http = require('http').createServer(app).listen(3001);
var io = require('socket.io')(http);
app.io = io;
module.exports = app;
require('./functions')
functions.js:
var io = require('./app').io;
io.on('connection', function(socket) {
console.log('a user connected');
);

Related

Using socket io without returning the index.html throws an error

I would like to setup websocket without necessarily having to return the index.html file
Am still new to the socket io and this is what i have tried
installed socket io via
npm install socket.io --save
created index.js
var http = require('http');
var fs = require('fs');
// Loading socket.io
var io = require('socket.io');
// When a client connects, we note it in the console
io.sockets.on('connection', function (socket) {
console.log('A client is connected!');
});
server.listen(1100);
Now when i run node index am getting an error
io.sockets.on('connection', function (socket) {
^
TypeError: Cannot read property 'on' of undefined
What am trying to do is connect the websocet to my vuejs client side so ive skipped the part to display html part since i dont want to display html but to use the socket events.
where am i going wrong?
Hey you need to attach socket.io to an http server for your code to work and listen to incoming events.
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
//io.on is the shorter form of io.sockets.on
io.on('connection', function(socket){
console.log('user connected');
});

In node.js do I need http when building a socket.io/express app?

I have just started using node.js and I can build a simple app that responds to requests and has some basic routing using the express framework.
I am looking to create something using socket.io but I am slightly confused over the use of the 'http' module. I understand what http is but I don't seem to need it for the following to work:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.htm');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
I can serve a html page over http without requiring the http module explicitly with something such as:
var http = require('http');
If I am using express do I have any use for the http module?
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
...
server.listen(1234);
However, app.listen() also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:
var express = require('express');
var app = express();
var socketio = require('socket.io');
// app.use/routes/etc...
var server = app.listen(3033);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
...
});
source
http://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen
no, you probably don't need it.
You can use something like:
var app = require('express').createServer();
var io = require('socket.io')(app);
//Your express and socket.io code goes here:

app.get returned undefined in Node/Express

I'm using express in my app.js I set something like this
var express = require('express');
var app = express();
app.set('myVar', 'hello');
then in my controller I want to get the value. I do
var express = require('express');
var app = express();
console.log(app.get('myVar')) // undefineded
Any idea why?
Your controller creates a new, fresh instance of Express. If you want to be able to share variables, you need to pass the instance from app.js to your controller:
// app.js
var express = require('express');
var app = express();
app.set('myVar', 'hello');
require('./controller')(app);
// controller.js
module.exports = function(app) {
console.log(app.get('myVar'));
};
EDIT: judging by the comments, the issue isn't so much passing app around, but moving parts of the application to separate modules. A common setup to enable that would look like this:
// app.js
var express = require('express');
var app = express();
app.set('myVar', 'hello');
app.use('/api', require('./controller/auth'));
// controller/auth.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
console.log(req.app.get('myVar'));
return res.send('hello world');
});
module.exports = router;
In your example you are instantiating a second app that you then try to get the value from. You need to get from the exact same object:
var express = require('express');
var app = express();
app.set('foo', 'bar');
app.get('foo');
If you are new to express, you can use the cli generator to scaffold out an application that shows you a sane pattern how to use the same express instance throughout your whole application.

Using socket.io with express 4 generator

New to node, so my apologies.
I'm working on my app and I want to send the location using socket.io. I've found 1000 examples, but all refer to when express had no routes, and it all was at the app.js. All examples refer to chat applications.
I was able to run an example piecing together several questions I searched but, I don't understand how to get the io that I finally got working on my app.js to interact with my index.js so I can use it with multiple emit parameters. express.io is outdated and I can't find anything current.
On bin/www
/**
* Socket.io
*/
var io = app.io
io.attach( server );
My app.js
var socket_io = require('socket.io');
var app = express();
var io = socket_io();
app.io = io;
So I can use:
io.on('connection', function (socket) {
console.log('IO Ready');
});
I don't know how to use the sockets on my index.js (routes), I can't modularize it.
Thanks in advance.
I have to say, I think the default generated project is bad for a socket.io setup. The node http.Server var is all the way in bin/www and not in app.js.
So the first thing is to move all the relevant stuff from bin/www to app.js. Mainly you just need
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
just like in the socket.io docs.
Now with io in app.js, we can use that when the routes are required. I forgot how exactly the default routes are set up, but I think they set up an app and just export it. Instead, you can set up something like
module.exports = function(app, io) {
io.on('connection', function(socket) {
console.log('connected!');
}
app.get('/foo', function() {
...
}
}
And now when you require the routes, instead of having the default
var index = require('./routes/index');
app.use(index);
or something of that accord, you can just do
require('./routes/index')(app, io);
And that's how you get io into your routes. Or at least how I do it anyway.

Socket.io + Express: Express declaration error

My requires:
//app.js Socket IO Test
var app = require('express').createServer(),
redis = require('socket.io/node_modules/redis'),
io = require('socket.io').listen(app);
My error:
Warning: express.createServer() is deprecated, express
applications no longer inherit from http.Server,
please use:
var express = require("express");
var app = express();
How do I modify my declarations to avoid this error? I understand this methodology is now deprecated for Express, just not sure what it needs to be changed to...
Thanks in advance!
Simply replace var app = require('express').createServer() with:
var express = require("express");
var app = express();

Categories

Resources