socket.io authentication with socketio-auth - node.js

I've a server in node.js & Express and i use socket.io for real-time messaging and socketio-auth module for authentication,I setup everything for using this module but i found nothing about db object and findUser that the auther used in examples(not even an single line of comment about them) to work with,should i implement them myself,right?
if anyone have a working example of using or implementing them with redis i'll be grateful to see it.

the db and findUser object is from MongoDb and not related to socket.io. I would recommend you look at http://www.tutorialspoint.com/mongodb/ to gain a better understanding as to why they are used

Related

expressJS exporting other things in addition to app

In my web application (I'm using expressJS), there are many services (such as mongoDB connection, MQTT connection, etc.) that need to be executed once the whole application is executed (using npm start command). Therefore, I can make use of these services in my entire application. For example, I want to use my MQTT connection in different files.
My idea is to export the MQTT connection, MongoDB connection, etc. in addition to the app this way:
//app.js
module.exports = {
app: app,
mqttConnection: myMQTTConnection,
db: myMongoDB
};
However, we know that this approach doesn't work (I tested it and got an error saying: TypeError: app.set is not a function).
How can I export other things in addition to app from app.js file?
If my approach is not possible, what other approaches can I use? (considering the fact that many services (such as connecting to a server, etc.) are asynchronous)

Console using postgres+sequelize with Node.js?

I am fairly new to Node but I am loving the tool. My only problem is when I want to have direct access to the database. I have a good experience with ruby on rails+postgres. Using rails console was very helpful when I was developing rails.
Is there some kind of equivalent I can use to have direct access to my database? I have uploaded my app to heroku so I would like something that I can run on heroku as well.
(I prefer not to use SQL, I am wondering if there is a sequelize console?)
Here is the way to do it:
node --experimental-repl-await
> models = require('./models');
> User = models.User; //however you load the model in your actual app this may vary
> await User.findAll(); //use await to avoid promise errors
TLDR
This gives you access to all of the models you have created and you can use the sequelize ORM commands like findAll, create etc.. just as you would in Rails active record.
Sequelize uses promises, so to run these properly in REPL you will want to use the --experimental-repl-await flag

MarkLogic - node.js - How to require cts for search

I have the node db connection working okay I think:
var marklogic = require('marklogic');
var db = marklogic.createDatabaseClient({ ...
And now I want to do cts.search
How do I require cts?
You don't. The cts functions are for running queries within the database. Working with the Node.js Client API, you'll use the query builder. There's an example in the feature introduction, with more detail in the Getting Started tutorial and the Node.js Application Developer's Guide.
You may be mixing up the Node.js Client API with Server-side JavaScript. The former is used to have middle-tier Node.js code interact with MarkLogic. The latter is for writing REST API extensions or transformations that run within the database (and can be called from the Node API).

Using websockets (socket.io) for sails 0.10.x

I am developing a node app on sails, my app needs to do regular exchange of data between server and client
The SailsCast video demonstrates the process for sails 0.9.x, and the mentioned files are not present in Sails 0.10.x
Also the sails doc says we should be using sails.socket instead of sails.io as the later will be deprecated in coming versions
Can any one help me in exchanging data between server and client through sockets. I have done it through node but with Sails MVC, i am not sure how to put the pieces together.
Any reference link or suggestion will be of great help
Thanks
I had the same trouble. You may want to check SailsSocket doc.
If you are loading sails.io.js in your page and haven't changed the default
io.sails.autoConnect to false, then a WebSocket should be created for you.
You can access it via io.socket
Create e.g. assets/js/dependencies/app.io.js with:
io.socket.on('connect', function socketConnected() {
console.debug("This is from the connect: ", io.socket);
console.debug("WebSocket is connected:", io.socket.isConnected());
});
Make sure to load this file after sails.io.js (See pipeline.js).
Open you browser console and double check.
Also check SailsSocket methods
Hope this help you get started

How to work with node.js and mongoDB

I read :
How do I manage MongoDB connections in a Node.js web application?
http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
How can I set up MongoDB on a Node.js server using node-mongodb-native in an EC2 environment?
And I am really confused. How I should work with mongoDB from node.js? I’m a rookie, and my question may look stupid.
var db = new db.MongoClient(new db.Server('localhost', 27017));
db.open(function(err, dataBase) {
//all code here?
dataBase.close();
});
Or every time when I needing something from db I need call:
MongoClient.connect("mongodb://localhost:27017/myDB", function(err, dataBase) {
//all code here
dataBase.close();
});
What is the difference betwen open and connect? I read in the manual that open: Initialize and second connect. But what exactly does that mean? I assume that both do the same, but in the other way, so when should I use one instead the other?
I also wanna ask it's normal that mongoClient needing 4 socket? I running two myWEbServer at the same time, here’s picture:
http://i43.tinypic.com/29mlr14.png
EDIT:
I wanna mention that this isn't a problem ( rather doubt :D), my server works perfect. I ask because I wanna know if I am using mongoDB driver correctly.
Now/Actually I use first option,init mongo dirver at the beginning and inside load put all code.
I'd recommend trying the MongoDB tutorial they offer. I was in the same boat, but this breaks it down nicely. In addition, there's this article on github that explains the basics of DB connection.
In short, it does look like you're doing it right.
MongoClient.connect("mongodb://localhost:27017/myDB", function(err, dataBase) {
//all code here
var collection = dataBase.collection('users');
var document1 = {'name':'John Doe'};
collection.insert(document1, {w:1}, function(err,result){
console.log(err);
});
dataBase.close();
});
You still can sign up for a free course M101JS: MongoDB for Node.js Developers, provided by MongoDB guys
Here is short description:
This course will go over basic installation, JSON, schema design,
querying, insertion of data, indexing and working with language
drivers. In the course, you will build a blogging platform, backed by
MongoDB. Our code examples will be in Node.js.
I had same question. I couldn't find any proper answer from mongo documentation.
All document say is to prefer new db connection and then use open (rather than using connect() )
http://docs.mongodb.org/manual/reference/method/connect/

Resources