Showing posts with label NodeJS-socket. Show all posts
Showing posts with label NodeJS-socket. Show all posts

Monday 20 July 2020

Call socket event from router page in Node

Call socket event from router page in Node

Question: How to send socket notification from Node API/Router?
  1. In App file (like index.js OR app.js)
    //Express setup
    var express = require('express');
    var app = express();
    
    //socketsetup setup
    var io = require('socket.io')(https);    
    app.set('io', io); //IMP this will let available io in router file
    
  2. In Router or Controller file
    exports.sendnotify = function(req, res) {
        var io = req.app.get('io');
        io.sockets["in"]('room_446').emit('session_end_notify', {vid:100,event_type:'live'});   //Send the Notification
        res.send('node is working fine');
    };
    
    


Question: How decrypt a text which is encoded with base64?
let buff = Buffer.from('ENCRYPTED_TXT', 'base64');
print buff.toString('ascii');        




Tuesday 15 May 2018

io.emit vs socket.emit vs socket.broadcast.emit


Send message to one client, like New user come, we show him "welcome message" 
socket.emit('message', "Welcome user to our Chat Group");


Sending to all clients except sender 
socket.broadcast.emit('message', "New user i.e joy joined our chat group."); 


Sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'sending to all clients in 'game' room(channel) except sender'); 


Sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'sending to sender client, only if they are in game room'); 


Sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'sending to individual socketid'); 


Sending to all clients, include sender
io.emit('message', "sending to all clients, include sender"); 


Sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'sending to all clients in game room'); 


sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'sending to all clients in namespace myNamespace');


for emiting to specific clients
io.sockets.socket('for emiting to specific clients'); 


send to all connected clients 
io.sockets.emit('send to all connected clients');


Tuesday 8 August 2017

Send Acknowledgment for socket.io event

Send Acknowledgment for socket.io event

Question: How to send Acknowledgment for socket.io event?
in node.js (Server side)
io.sockets.on('connection', function(socket){    

    /* Message Received from Client*/
    socket.on('chat_messsage', function(data, callback){        
        var responseData = { 'message':data.message, 'received':1};        
        callback(responseData); //Send Acknowledgment to the client
    });


});

Client Side JavaScript
var socket = io.connect('http://localhost:3000');
socket.on('connected', function (data) {    
    
    //Send message to Server with callback function
    socket.emit('chat_messsage', {message:"this is message"}, function(response){
        console.log(response); //console the Acknowledgment
    });

});



Question: How to Send additional data on socket connection?
Client side, connect with parameter
  var socket =  io.connect(chatURL,{ query: "req_from=browser" });


Sever side, Detect the client Connection.
io.on('connection', function (socket) {
  console.log(socket.handshake.query['req_from']);
});


Question: How to connect node with http and https?
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('cert/server.key', 'utf8');
var certificate = fs.readFileSync('cert/server.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

//http connect
httpServer.listen(8080);
//https connect
httpsServer.listen(8443);



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');