Tuesday, 20 June 2017

Socket.IO module with nodeJs tutorial

How to send message from Node(server) to browser(client) and vice versa? How to connect/disconnect to  the room.

Question: What is socket.io module?
Socket.IO is a JavaScript library for realtime web applications.
It enables realtime, bi-directional communication between web clients and servers.
It has two parts
a) client-side library that runs in the browser.
b) server side that run in browser.



Question: Give example of real time application?
  1. Instant messengers like facebook notification/message.
  2. Push Notifications like when someone tags you in a picture on facebook, you receive an instant notification.
  3. Collaboration applications like multiple user editing same file.
  4. Online gaming




Question: Why we use socket.io?
  1. bi-directional communication channel between a client and a server.
  2. When an event occur, socket get notify and let to know the single client OR all clients.
  3. It is trust application and used by Microsoft Office, Yammer, Zendesk, Trello, and numerous other organizations.



Question: How to install socket.io in Web and Server?
In Browser (client)
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>

In NodeJS
npm install socket.io

var io = require('socket.io')




Question: How get to know, if user is connect/disconnect?
In Server(Node)
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

//Whenever someone connects (execute when come in Web page)
io.on('connection', function(socket){
  console.log('A user connected');

  //Whenever someone disconnects (execute when Leave the web page)
  socket.on('disconnect', function () {
    console.log('A user disconnected');
  });

});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

In Browser
 var socket = io.connect('http://127.0.0.1:3000'); 



Question: How to get to know, when new user comes in browse?

OR
Question: How to send message from Client(Browser) to Server(Node)?
In Server
 
socket.on('adduser', function(name, email, uid) {
    //name, email, and uid of user
    console.log('New user comes in web page');
});

In Browser>
 
socket.emit('adduser', 'Robin Sharma','robinsharma@no-spam.ws',2555);      




Question: How to send message from Server(Node) to Client(Browser)?
In Server
socket.emit('newRequestCome', 'name','myself send request'); //Send message to socket (Same window)
socket.broadcast.to(roomName).emit( 'newRequestCome','name','message'); //Send message to all socket (Other windows in same Room)

In Browser
 
socket.on('newRequestCome', function(name, message) {                        

});


Question: How to join the room and send message?
Join the room
io.on('connection', function(socket){
  socket.join('someNo12');
});



Question: How to send message the room?
Send message to the room
io.in('someNo12').emit('some event');
//io.to('someNo12').emit('some event');



Monday, 19 June 2017

underscore module with nodeJs tutorial

Underscore is a JavaScript library that provides a lot of useful helper functions to fast the node development. following are list of available array/object functions.

Question: What is underscore module?
Underscore is a JavaScript library that provides a lot of useful functional helpers without extending any built-in objects.



Question: How to install async module?
npm install underscore




Question: How to include request module in node project?
var _ = require("underscore");




Question: Give an example of each in underscore?
 
_.each(['Understand', 'underscore', 'Module'], function(value,pos){
    console.log(value+' stored at '+pos);    
});

Output
Understand stored at 0
underscore stored at 1
Module stored at 2




Question: What are other collections in underscore module?
each
map
reduce
reduceRight
find
filter
where
findWhere
reject
every
some
contains
invoke
pluck
max
min
sortBy
groupBy
indexBy
countBy
shuffle
sample
toArray
size
partition



Question: How to create a range array?
 
var lists=_.range(5);
console.log(_.first());




Question: How to get first nth element from array?
 
var lists=["One","two","three","four", "five"];
console.log(_.first(lists)); //One
console.log(_.first(lists,2)); //One two
console.log(_.first(lists,3)); //One two three




Question: How to check a variable is empty OR Not ?
var name=''
console.log(_.isEmpty(name));//true

name='Hello User'
console.log(_.isEmpty(name));//false




Question: What are array functions in underscore module?
first
initial
last
rest
compact
flatten
without
union
intersection
difference
uniq
zip
unzip
object
indexOf
lastIndexOf
sortedIndex
findIndex
findLastIndex
range



Question: How to get key value of an object data?
 
var lists={1:"One",2:"two",3:"three",4:"four", 5:"five"};
console.log(_.keys(lists)); //1,2,3,4,5




Question: What are the object functions in underscore module?
 
keys
allKeys
values
mapObject
pairs
invert
create
functions
findKey
extend
extendOwn
pick
omit
defaults
clone
tap
has
matcher
property
propertyOf
isEqual
isMatch
isEmpty
isElement
isArray
isObject
isArguments
isFunction
isString
isNumber
isFinite
isBoolean
isDate
isRegExp
isError
isNaN
isNull
isUndefined