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.
Related
I'm been looking around everywhere for the last few days for a way to access my socket IO instance running in Express from my routes and have not found a working solution.
The problem here is that I only run my socket IO instance at run-time and my routes and defined before that obviously.
The most promising solution I have found is one that wraps my routes in a function and requires it in my app.js file with whilst passing in the IO instance as an argument like so var routes = require('routes')(io)
Here is my setup:
app.js
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
io.on('connection', function(client) {
console.log(io.sockets.sockets)
});
app.set('port', 7777);
const routes = require('./routes/index')(io);
app.use('/', routes);
app.listen('7777');
routes.js
module.exports = function(io) {
const express = require('express');
const router = express.Router();
router.get('/test', (req, res) => {
console.log(io.sockets.sockets);
});
return router;
}
If I connect to the WebSocket, my io.on event fires and I get a console log of the connected sockets in the form of a fairly large object that contains the socket id's etc.
If however, I get to the route '/test' I get a console log of a blank object: {}. I would imagine this is because the instance of the socket I am passing down to the routes does not have anyone connected to it at the time hence the blank object is returned, but this is just my best guess.
Where I'm a little stuck is how to get the full instance with all live connections to the route.
Alternatively, I've thought of attaching the io instance to the request object like so in order to have access to it in the routes:
server.on('request', function(req, res){
req.io = io;
}
but couldn't quite get it to work, nor am I sure this is the correct approach.
I imagine this must be a common issue so I would love a clear answer on how to work around this and the correct approach to tackle the issue.
EDIT
So I eventually got my above code working, I hit the '/test' endpoint from an AJAX GET request within the chrome extension instead of my just visiting the URL localhost:7777/test. What I can't understand here is why it works with an AJAX request but not page navigation? (The page navigation is done after I make the socket connection in the extension)
This code runs in the server. I am making a simple websocket on the server and it looks for connections made to it. However, IntelliJ does not recognize the on() method that has been called on io. I am using IntelliJ latest version and coding in Node.js
var http = require('http');
var express = require('express');
var socket = require('socket.io');
function onRequest(req,res)
{
console.log('User requested for page: ',req.url);
}
// create a middleware application
var app = express();
app.use(onRequest);
// serve static files
app.use(express.static('public'));
var server = http.createServer(app).listen(4000);
// setup the socket on the server
var io = socket(server);
io.on('connection',function(socket)
{
console.log('Socket id is: ',socket.id);
});
Try npm install #types/socket.io. It will add the necessary definition file.
I'm building a node.js server and my folder structure looks like this:
server.js
app/routes.js
app/routes/users.js
My problem is that i'm not sure how can i use the app variable inside the users.js file. Do i have to require and setup express again in this file or is there a better/easier way to do it? Here is my sample code(just the bare minimum to understand my problem):
server.js
// Include our packages in our main server file
var express = require('express');
var stormpath = require('express-stormpath');
var app = express();
// Init Stormpath for user management and authentication
app.use(stormpath.init(app));
// Load routes
require('./app/routes')(app);
// Start the server
app.listen(process.env.PORT);
// Stormpath will let you know when it's ready to start authenticating users.
app.on('stormpath.ready', function () {
console.log('Your server is running on port ' + port + '.');
});
app/routes.js
// Import dependencies
const express = require('express');
const stormpath = require('express-stormpath');
// Export the routes for our app to use
module.exports = function(app) {
// Create API group routes
const apiRoutes = express.Router();
// User management: get users, invite users, view user profile
var UsersRoute = require('./routes/users');
apiRoutes.get('/memberinfo', stormpath.loginRequired, UsersRoute.memberInfo);
// Set url for API group routes
app.use('/', apiRoutes);
};
app/routes/users.js
// Protected route test
module.exports.memberInfo = function(req, res){
//how do i access the "app" here?
res.status(200).send({ user: req.user });
}
In your .memberInfo method, you can use req.app to access the app object that is associated with that request.
In cases where you aren't passed a req object that you can use in this way, then you need to initialize the module by calling a method on it and passing it the app object and the module can then store the app object locally so it can use it when desired.
var http = require("http").Server(express);
var io = require("socket.io")(http);
server.listen(8080);
Above code work if I put in different route. But how to create an instance of it so that I don't have to declare many times?
you are not clear with your question but i think you want to you io object in different files
you can do this using
app.all("*",function(req,res,next){
req.io=io;
next();
});
define this middleware before the routing in you server file
and where you want to use this you can get this object as
function(req,res,next){
var io = req.io;
}
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');
);