Setting up quickblox with node
In this post I am going to set a node server to use Quickblox.
brew uninstall node
npm install -g express
- Starting from express 4.00 you also need to install express generator as mentioned here.
npm install -g express-generator
- Generate new project:
express chat
- Install node modules:
cd chat/ && npm install
- Git-ignore
node_modules
directory - Start server:
DEBUG=chat:* npm start
- Check if its working localhost:3000
- Install http status codes:
npm install http-status-codes --save
-
Modify
routes/index.js
to return json instead of renderingindex.html
:router.get('/', function (req, res, next) { res.status(HttpStatus.OK).json({title: 'Express'}); });
- Restart server and test.
- Install quickblox:
npm install quickblox --save
-
- Modify
routes/index.js
to initialize quickblox:
var QB = require('quickblox'); // initalise the environmenet with my application id, authentication key and authentication secret QB.init(<CLIENTID>, <KEY>, <SECRET>); // create an user session var params = {login: 'system', password: 'welcome123'}; QB.createSession(params, function (err, result) { if (err) { console.log('Something went wrong: ' + err); } else { console.log('Session created with id ' + result.id); } }); /* GET home page. */ router.get('/', function (req, res, next) { // list the users currently enrolled QB.users.listUsers(function (err, result) { for (var i = 0; i < result.items.length; i++) { console.log('User ' + result.items[i].user.login + ' is registered'); } }); res.status(HttpStatus.OK).json({title: 'Express'}); });
- Modify
- Restart and test if users are getting printed.
-
Shift the QB initialisation code to
app.js
and make QB a global var as mentioned here, because we have to useQB
in multiple routes.//Initialize quickblox QB = require('quickblox'); // initalise the environmenet with my application id, authentication key and authentication secret QB.init(31620, 'HGKzgragj54ktge', 'YeQ-bXJ7asULOU-'); // create an user session var params = {login: 'system', password: 'welcome123'}; QB.createSession(params, function (err, result) { if (err) { console.log('Something went wrong: ' + err); } else { console.log('Session created with id ' + result.id); } });
- Restart and test if users are getting printed.
References
- Node
- Quickblox
- Express
- express command not found in bash after installing it with npm
- Global Variable in app.js accessible in routes?
- Build a RESTful API Using Node and Express 4
- Node.js - RESTful API
- npm - QuickBlox JavaScript SDK
- Guide for running with node.js, express, jade, and mongodb
- npm-http-status-codes
- How can run app using Express 4 without ‘DEBUG=node:* ./bin/www’
- Quickblox - js sdk