I would like use nodeJS to refresh my view, every time a function has made changes to the database. If we take MEAN-stack as an example, I don't want to send an $http-request every x seconds to check if changes have been made to the database. I would like the front end to get notified automatically and then update the view.
What are best practices for this? I would use some kind of Oberserver pattern in the server side, but do not know how I could notify the front end with that.
To get the front end to get notified automatically and then update the view you could use Socket.io framework.
You can find all of the documentation on their site: http://socket.io/
And here is a basic example:
app.js ( to set up the server)
var http = require('http');
var express = require('express');
var port = normalizePort(process.env.PORT || '1000');
var app = express();
var server = http.createServer(app);
server.listen(port);
io = require('socket.io')(server);
///ROUTES
var routes = require('./routes/index')(io);
var users = require('./routes/users');
///////
I pass the io object to route index(and ofcourse there is a lot more stuff on app.js..this is just a basic example...).
mysql.js (to create a pool for connections)
var mysql = require("mysql");
var pool = mysql.createPool({
host : 'host',
user : 'user',
password : 'pass',
database : 'db_name',
connectionLimit: 1000
});
exports.pool = pool;
index.js
module.exports = function(io) {
var express = require('express');
var router = express.Router();
var mysql = require('../mysql.js').pool;
io.on('connection', function (socket) {
socket.on('event_name', function (data) {
mysql.getConnection(function(err,connection){
if (err) {
connection.release();
return;
}
connection.query("SQL STUFF",function(err,rows){
if(rows.length>0){//checks if there are more than 0 rows returned.....
socket.emit('do_something',data_you_want_to_pass);
}
else{
socket.emit('do_something_else',data_you_want_to_pass);
}
connection.release();
});
connection.on('error', function(err) {
return;
});
});
});
});
router.get('/', function(req, res) {
res.render("index");
});
return router;
}
And then on html page you have socket.emit and socket.on again.....
I recommend you take a look at the documentation and a few other examples...
I hope I helped you.
Related
I am on the process of building a chat application with nodejs, reactjs mongo and socket.io.My chat app consists of both one to one and group chats.I have built a schema for group chat and i am inserting group names along with its members and their chats in the table.Since im a beginner towards socket.io, I dont know where to put the socket logic that needs to be fired after the db post operation.Can some one suggest any examples for me?
Update your code accordingly:
=> server.js file
// Declare socket.io
const io = require('socket.io')(server);
// Add middleware to set socket.io in
app.use((req, res, next)=>{ res.locals['socketio'] = io; next(); });
=> In your controller file
// Get the value of socket.io
module.exports = your_function_name = (req, res) => {
const io = res.locals['socketio']
// Use io when you need.
});
Hope this solves your query.
You can separate you socket related code by following way :
==>app.js
var express = require('express');
var socket = require('./socketServer');
var app = express();
var server = app.listen((config.node_port || 3000), function () {
console.log('Listening on port ' + (config.node_port || 3000) + '...');
});
socket.socketStartUp(server);
module.exports = app;
==>socketServer.js
var io = require('socket.io')();
var socketFunction = {}
socketFunction.socketStartUp = function (server) {
io.attach(server);
io.on('connection', function (socket) {
console.log("New user is connected with socket:", socket.id);
})
}
module.exports = socketFunction;
You can also check node API startup code with socket functionality in below link:
Node API Start up
Hope this answer is helpful to you
I'd like to know how to work with connectivity to a database in MEAN stack application. In particular, when should I create a connection to a database and when should I destroy a connection to a database. Should I create and destroy a connection on every new HTTP request or should I store a once created connection and use it for any subsequent requests as long as possible. I use Mongoose as a modeling tool.
Here is an example.
This is my routes.js file with a route /index. A request to this route should fetch some date from MongoDb database. It bothers me how I connect and disconnect to a database now. Yes, I connect and disconnect to a database exactly as written in Mongoose docs, but it it the right way to do it in a serious production environment?
var express = require('express');
var router = express.Router();
var config = require('./db-config');
// I create a Mongoose instance as a module object,
// as opposite to create it in every request handler function below.
var mongoose = require('mongoose');
var productSchema = require('../db/productSchema'); // model schema is also a module-wide object
// And here is a request handler function.
// It is called on every request as a brand new.
// I create and destroy a database connection inside this request handler
router.get('/index', function(req, res, next) {
// I connect to a database on every request.
// Do I need to do it here in a request handler?
// May I do it outside of this request handler on a module-wide level?
mongoose.connect('mongodb://my_database');
// I create a new connection here in a request handler.
// So it lives only during this request handler run.
// Is this the right way? May I do it outside of this request handler
// on a module-wide level and somehow keep this connection and use it
// in every subsequent requests to this or any other route in the app?
var db = mongoose.connection;
db.on('connecting', function() {
console.log('connecting');
});
db.on('connected', function() {
console.log('connected');
});
db.on('open', function() {
console.log('open');
});
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function(cb) {
var Product = mongoose.model('Product', productSchema);
Product.find({category: "books"}, function(err, prods) {
if (err) return console.error(err);
// I close a connection here in a callback.
// As soon as successfully fetched the data.
// Do I need to close it after every request?
// What is the right place and time to do it?
db.close(disconnect);
res.json(prods);
});
});
})
Found some good answers:
https://softwareengineering.stackexchange.com/questions/142065/creating-database-connections-do-it-once-or-for-each-query
What are best practices on managing database connections in .NET?
Its best practice to have your db connection in a separate module (db.js)
var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/dbname', function(){
console.log('mongodb connected')
})
module.exports = mongoose
Each model should have a separate module that takes in the db connection (post.js)
var db = require('../db.js')
var Post = db.model('Post', {
username: {type: String, required: true},
body: {type: String, required: true},
date: { type: Date, required: true, default: Date.now }
})
module.exports = Post
Then whenever you need to use that data set just require it and make calls
var Post = require('/models/post')
Post.save()
Post.find()
This is an opinion based question I'd say. What I use for my app is
app.get('/', function (req, res) {
res.sendfile('index.html');
});
mongoose.connect('mongodb://localhost:27017/my_db');
This way I create a connection once rather than on every HTTP request. Your way should work fine but it seems you will have to connect and disconnect the db to your app way too many times specially when the app is in development.
You want your connection to act like a singleton so as mentioned in the answer above it makes sense to do it outside of, and preferable before your routes:
var compression = require('compression');
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
...
app.use(compression());
// db
var mongoose = require('mongoose');
var configDB = require('./config/database.js');
mongoose.connect(configDB.url); // connect to our database
config/database.js:
module.exports = {
'url' : '#localhost:27017/dbname'
};
This is my solution :
import express from 'express';
import mongoose from 'mongoose';
import { name } from '../package.json';
import * as localconfig from './local-config';
import debug from 'debug';
debug(name);
const app = express();
const port = process.env.PORT || 3000;
const mongoUrl = localconfig.credentials.MONGO_URL;
import usersRoutes from './routes/users/user-routes';
app.use('/v1/users', usersRoutes);
mongoose.connect(mongoUrl)
.then(() => {
debug('DB connection successful');
app.listen(port, '0.0.0.0', () => {
debug(`Running on port ${port}`);
});
})
.catch((err) => {
debug(err);
});
You should first check weather the connection is successful or not and only then listen to a certain port. This is my app.js file where all the routes are loaded, so you do not have to call the db connection in all your files. You have a single config file where all the config is done. Your router file user-routes.js will look something similar to this:
import express from 'express';
import User from '../models/user'
const router = express.Router();
router.get('/', (req, res, next) => {
User.find()
.then((response) => res.json(response))
.catch((err) => next(err));
});
module.exports = router;
I have an express server, and while building it created several "helper" functions on their own routes. I'd like those routes to be accessed on a different port. Is there anyway to do this in express?
In the code below, the "/factory" route (and other functionality) would be on one port, and the helper routes of "/killallthings", "/listallthings", and "/killserver" would be on a separate port.
Here is a simplified version of the code:
var express = require('express');
var things = [];
var app = express();
var port = 8080;
app.post('/factory/', function(req, res) {
//Create a thing and add it to the thing array
});
//Assume more functions to do to things here....
app.post('/killallthings/', function(req, res) {
//Destroy all the things in the array
});
app.post('/listallthings/', function(req, res) {
// Return a list of all the things
});
app.post('/killserver/', function(req,res){
//Kills the server after killing the things and doing clean up
});
//Assume https options properly setup.
var server = require('https').createServer(options, app);
server.listen(port, function() {
logger.writeLog('Listening on port ' + port);
});
Is this possible with express?
Based on Explosion Pills suggestion above, I modified the code in roughly this way:
var express = require('express');
var things = [];
var app = express();
var admin_app = express();
var port = 8080;
var admin_port = 8081;
app.post('/factory/', function(req, res) {
//Create a thing and add it to the thing array
});
//Assume more functions to do to things here....
admin_app.post('/killallthings/', function(req, res) {
//Destroy all the things in the array
});
admin_app.post('/listallthings/', function(req, res) {
// Return a list of all the things
});
admin_app.post('/killserver/', function(req,res){
//Kills the server after killing the things and doing clean up
});
//Assume https options properly setup.
var server = require('https').createServer(options, app);
server.listen(port, function() {
logger.writeLog('Listening on port ' + port);
});
var admin_server = require('https').createServer(options, admin_app);
admin_server.listen(admin_port, function() {
logger.writeLog('Listening on admin port ' + admin_port);
});
I wish I knew how to give Explosion Pills the credit for the answer! :)
If you are trying to create multiple servers then why not crate multiple bin/www files with different ports and configurations. Another way could be pass port number directly from command line.
Now edited with solution
I'm writing a web page, backed by Node and Express, which uses the twitter streaming API to plot the location of tweets containing a given word onto a Google map. The page connects to the server and places each tweet with location that the server emits onto the map.
the node js application is this. twitFactory just gives me a twitter object from the Twitter npm module with my various connection strings.
'use strict';
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var twitFactory = require('./lib/twitFactory');
var twit = twitFactory.twit();
var searchTerm = process.argv[2];
var locations = '-180,-90,180,90';
server.listen(3000);
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
twit.stream('filter', {locations: locations}, function (stream) {
stream.on('data', function (tweet) {
if (tweet.text && tweet.text.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1) {
if (tweet.text && tweet.coordinates && tweet.coordinates.coordinates && tweet.coordinates.type === 'Point') {
socket.emit('tweet', { text: tweet.text, lon: tweet.coordinates.coordinates[0], lat: tweet.coordinates.coordinates[1] });
}
}
});
});
});
I have this working for a single web client, which is fine for my development environment, but I want to avoid re-initialising the Twitter connection inside the on connection event every time a new client connects, or I would quickly hit my API call limit.
I think I want to define the twitter stream separately, and then add new connections to the socket's subscribers, but can't work out how to code this, without the nested structure you see above.
(I realise that it's very inefficient to get all tweets with location then filter by content, but very few tweets have location attached, and the API does not currently let you specify location AND search term, it gives you all tweets with either.)
UPDATE:
I found a solution which was to use a different Twitter Node module which allows the stream object and events to be declared separately. it's twit (as opposed to twitter) in npm. the code now looks like this:
'use strict';
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var logger = require('./lib/logger');
var twitFactory = require('./lib/twitFactory');
var searchTerm = process.argv[2];
var locations = ['-180','-90','180','90'];
var twit = twitFactory.twit();
var tweetStream = twit.stream('statuses/filter', {locations: locations});
server.listen(3000);
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
console.log('new connection');
tweetStream.on('tweet', function (tweet) {
if (tweet.text && tweet.text.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1) {
if (tweet.text && tweet.coordinates && tweet.coordinates.coordinates && tweet.coordinates.type === 'Point') {
socket.emit('tweet', { text: tweet.text, lon: tweet.coordinates.coordinates[0], lat: tweet.coordinates.coordinates[1] });
}
}
});
});
I'm trying to check the username in the NodeJs chat with the user session. Is there a secure way to do it? (My NodeJs server isn't in the same place as the host of the website). I'm using this code NodeJs + express to do the chat application.
Code:
var express = require('express');
var app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server, { log: false });
server.listen(8080);
app.get('/chat.js', function(req, res) {
res.sendfile('chat.js');
});
/* CHAT FUNCTIONS */
io.sockets.on('connection', function (socket) {
socket.on('send', function (data) {
// I wanna get the user session
// something like this:
// (code below does not exist)
var username = session['username'];
console.log(username + " is here.");
io.sockets.emit('message', data);
});
});
How can I do this?
You need to use common storage for sessions e.g. memcached or redis.
Than u will have common access.