Repeated Question, But i found no perfect answer as a solutions. Please help me to solve this issue.
My App.js
var routes = require('./routes/index');
var users = require('./routes/users');
var media = require('./routes/media');
var widget = require('./routes/widget');
var display = require('./routes/displays');
var device = require('./routes/deviceinfo');
app.use('/', routes);
app.use('/display', display);
var server = http.createServer(app);
var io = require('socket.io')(server);
io.set('transports', ['polling', 'websocket']);
Display Routes: "routes/display.js"
I have code to insert a display, As soon a display is created an event should emit to the list of devices.
Emit should need to write in save success callback Method.
var express = require('express');
var router = express.Router();
router.post('/save', action_save_displays);
function action_save_displays(req, res){
display.save(req,body , function(err , display){
if(!err){
res.json(display);
//io.sockets.emit("displaycreated", display);
}
})
}
module.exports = routes;
How to pass io.sockets.emit in routes/display.js file. really appreciate your guidance. how to send socket object to routes to enable emit event.
You can swap the requires around a bit, and just pass socket.io to the display.js file
var routes = require('./routes/index');
var users = require('./routes/users');
var media = require('./routes/media');
var widget = require('./routes/widget');
var server = http.createServer(app);
var io = require('socket.io')(server);
var display = require('./routes/displays')(io); // here
var device = require('./routes/deviceinfo');
app.use('/', routes);
app.use('/display', display);
io.set('transports', ['polling', 'websocket']);
and get it in the display.js file
var express = require('express');
var router = express.Router();
module.exports = function(io) {
router.post('/save', function(req, res) {
display.save(req.body , function(err, display){
if(!err){
res.json(display);
io.sockets.emit("displaycreated", display);
}
});
});
return router;
}
Related
i'm little stuck on my express application.
I have a file index.js in routes folder and I want to save the result of mydata
var express = require('express');
var router = express.Router();
router.get('/', token, function(req, res, next) {
var mydata = req.userId
res.render('index', {
title: 'Welcolme',
});
});
module.exports = router;
And, in my other file (which depends on app.js), I would like to get and pass mydata to the server
var app = require('../app');
server.listen(port, function () {
require('../assets/js/server/socket')(server, mydata);
});
(Here is my app.js)
var express = require('express');
var indexRouter = require('./routes/index');
var path = require('path');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'twig');
app.use('/', indexRouter);
module.exports = app;
And the socket file
var io = {};
module.exports = (server, mydata) => {
console.log('Hello : ' , mydata)
io = require('socket.io')(server);
io.on('connection', (socket) => {
console.log('Connected')
});
};
Is there a way to do that?
Thank you :)
Maybe instead of still having this route.get('/') ...
You can let the user enter the data and have it transferred directly through the WebSocket connection.
Example:
//Client-side
//Get the user data somewhere from the HTML page
const userData = document.getElementbyId("some html element's id here").innerHTML;
//Send it to the back end
socket.emit('userData',userData)
//Open a listener to get the data from the back end later
socket.on('processedData',processedData=>{
//Use it in the way you desired.
});
//Server-side
io.on('connection', (socket) => {
console.log('Connected')
socket.on('userData',userData=>{
//Do something here, like validation and stuff
socket.emit('processedData','processedData here')
});
});
I'm trying to emit an event with socket.io on my Node Js server and receive it client-side but I can't get it to work.
This is my server code:
// require our dependencies
var express = require('express');
var expressLayouts = require('express-ejs-layouts');
var bodyParser = require('body-parser');
var app = express();
var port = 3000;
var server = require("http").Server(app);
var io = require('socket.io')(server);
// use ejs and express layouts
app.set('view engine', 'ejs');
app.use(expressLayouts);
// use body parser
app.use(bodyParser.json({extended : true}));
// route our app
var router = require('./app/routes');
app.use('/', router);
// set static files (css and images, etc) location
app.use(express.static(__dirname + '/public'));
// start the server
app.listen(port, function() {
console.log('app started');
});
This is my routes.js code, where the magic should happen:
module.exports = function(io){
// require express
var express = require('express');
var path = require('path');
// create our router object
var router = express.Router();
// export our router
//module.exports = router;
router.post("/", function(request, response) {
console.log(request.body);
io.emit('getupdate',{hello:'world'});
});
// route for our homepage
router.get('/showevent', function(req, res) {
res.render('pages/showevent',{data:exportage});
});
return router;
}
While this is my client-side script:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('getupdate', function (data) {
console.log(data);
});
</script>
On the routes.js file, to get the "io" variable i wrapped all inside a function.
I can't get the message emitted in case of a POST request on my client.
Could you help me to find the issue here ?
Try this:
// require our dependencies
var express = require('express');
var expressLayouts = require('express-ejs-layouts');
var bodyParser = require('body-parser');
var app = express();
var port = 3000;
var server = app.listen(port);
var io = require('socket.io').listen(server);
I am not sure if you have updated this in your own code, but this snippet should pass io to the router:
// route our app
var router = require('./app/routes')(io);
app.use('/', router);
I have a small project with NodeJS Express server and Angular2 frontend. The server has an API interfaces. The main part of this is: /api/alert. I want to do the following: If the /api/alert get an request then socket.io broadcast an event to all connected clients.
My server structure is the following:
server.js
var http = require('http'),
express = require('express'),
app = module.exports.app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
db = require('./config/db'),
path = require('path');
mongoose.Promise = global.Promise;
mongoose.connect(db.url);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'dist')));
var routes = require('./server/routes/index');
routes(app);
var server = http.createServer(app);
var io = require('socket.io')(server);
server.listen(port);
console.log('Server started on: ' + port);
server/routes/index.js
const deviceRoute = require('./devicesRoute');
const alertRoute = require('./alertsRoute');
module.exports = function(app) {
deviceRoute(app);
alertRoute(app);
}
server/routes/alertRoute.js
'use strict';
module.exports = function(app) {
var alertsService = require('../services/alertsService');
app.route('/api/alert')
.post(alertsService.createAlert);
};
server/service/alertService.js
'use strict';
exports.createAlert = function(req, res) {
// do something in database
// I want to broadcast to all client HERE
};
I can't pass the io and server variables to the function (from server.js). How can I do that? What is the easiest way?
Thank you!
First you would want to export both the app and server objects from server.js and pass your socket to your response in middleware as follows:
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
app.use(function(req, res, next){
res.io = io;
next();
});
...
module.exports = {app: app, server: server};
You can then require the server instance you created in server.js (depending on where you are requiring it from) as
var server = require('../server').server;
Since you added socket.io to the response object, you can use it in your services as
'use strict';
exports.createAlert = function(req, res) {
// do something in database
// I want to broadcast to all client HERE
res.io.emit("broadcast", "clients");
};
I would like to use socket.io and express routes and I have the following code:
app.js
var
http = require('http'),
path = require('path'),
passport = require('passport'),
userpassport = require('./lib/strategies/local'),
githubpassport = require('./lib/strategies/github'),
flash = require('connect-flash'),
express = require('express'),
logger = require('morgan'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
errorhandler = require('errorhandler'),
csrf = require('csurf'),
favicon = require('serve-favicon'),
sockets = require('./lib/socket'),
app = express();
var routes = require('./routes')();
app.get('/', routes.index);
var server = http.createServer(app).listen(app.get('port'), function () {
var io = require('socket.io')(server);
io.on('connection', function (iosocket) {
sockets.setSocket(iosocket);
});
});
lib/socket.js
var socket;
exports.setSocket = function(iosocket) {
console.log('setting socket');
socket = iosocket;
}
exports.getSocket = function() {
console.log('getting socket');
return socket;
}
routes/index.js
var sockets = require('../lib/socket');
module.exports = function () {
var routes = {};
routes.index = function (req, res) {
var socket = sockets.getSocket();
socket.on('app/create', function (data) {
console.log('got app create');
});
}
This does not work, I think it's because the setSocket function that appears on app.js runs after the page was loaded, therefore when I getSocket on the route it's not the latest socket (I can console log the given socket on the route, I can see there's a socket there, but I can also see that the setSocket runs afterwards).
How can I solve this?
Why doesn't the following work ?
app.js
reports/
├── index.js
└── batch.js
in app.js :
app.use('/reports', require('./reports')
in index.js :
var express = require('express');
var batch = require('./batch');
var app = express.createServer();
...
app.use('/batch', batch);
module.exports = app;
in batch.js :
var express = require('express');
module.exports = function() {
var app = express.createServer();
console.log('I am here');
app.get('/', function(req, res) {
console.log('I am there');
});
return app;
};
calling GET /reports/batch prints I am here but doesn't print I am there
Can anyone pinpoint me to the problem ?
Thanks
try this:
in app.js :
var express = require('express'),
http = require('http'),
path = require('path');
var app = express.createServer();
require('./reports')(app);
in reports/index.js :
module.exports = function(app){
var batch = require('./batch')(app);
app.use('/batch', batch);
}
in batch.js :
module.exports = function(app) {
console.log('I am here');
app.get('/', function(req, res) {
console.log('I am there');
});
};
Note that you may need to modify the app.get routing as needed. but basically the idea here is instead of calling createServer all the time just keep passing it down the chain from one module to the next.
Hope this helps!
Here is my app.js. It passes db and app to the feature.js. Basically they share the same app variable.
in app.js
var express = require('express'),
http = require('http'),
path = require('path'),
mongoose = require('mongoose'),
db = mongoose.connect('mongodb://abc:123#xxxx.mongohq.com:90000/app123423523');
var app = express();
//here you set app properties
require('./routes/feature').with(app, db);
in feature.js
module.exports.with = function(app, db) {
//do work
}
It seems that I have forgotten javascript a little.
I was doing
app.use('/batch', batch);
batch is equal to
function() {
var app = express.createServer();
console.log('I am here');
app.get('/', function(req, res) {
console.log('I am there');
});
return app;
};
Instead I should have done
app.use('/batch', batch());
Which equals to what express.createServer() returns which is what app.use expects to get