Real Time Chat Application
Real Time Chat Application
IN
“REAL TIME CHAT APPLICATION”
A PROJECT SUBMITTED IN PARTIAL FULFILLMENT OF
THE REQUIREMENT FOR THE DEGREE OF
Submitted By-
MUKESH KUMAR MAHATO 20408240026
Under Guidance of
Miss Momita kundu
in fulfilment of the requirement for the final year Diploma project in department of
Computer Science And Information technology. Government Polytechnic Jagannathpur is an
authentic work.
carried out by him under my supervision and guidance.
Date:
We thank all our friends and all those who have helped us for
carrying out this work directly or indirectly without whom
completion of this project work was not possible.
DECLARATION
HTML id Attribute:
• The HTML id attribute is used to specify a unique id for an
HTML element.
• You cannot have more than one element with the same id
in an HTML document.
• EXAMPLE:
<html><head><style>
#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
}
</style></head><body>
<h1 id="myHeader">My Header</h1>
</body>
</html>
HTML Iframes:
• An HTML iframe is used to display a web page within a web
page.
• The HTML <iframe> tag specifies an inline frame.
• <iframe src="url" title="description"></iframe>
• Iframe - Set Height and Width
• <iframe src="demo_iframe.htm" height="200" width="300"
title="Iframe
Example"></iframe>
• Iframe - Remove the Border
• <iframe src="demo_iframe.htm" style="border:none;"
title="Iframe
Example"></iframe>
• Iframe - Target for a Link
• <iframe src="demo_iframe.htm" name="iframe_a"
title="Iframe
Example"></iframe>
HTML File Path:
• File Path Examples
HTML ENTITIES:
<!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>talk</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Baloo+2:wght@500&family=Poppins
&display=swap" rel="stylesheet">
</button>
</header>
<div class="container">
<div class="users-window">
<div class="users-head">
<h3>people</h3>
<h4>3</h4>
</div>
<div class="users-list">
<p>jarvis</p>
<p>ultron</p>
<p>veronica</p>
</div>
</div>
<div class="chat-window">
<div class="chats">
<div class="user-join">
<p><b>jarvis</b> joined the chat</p>
</div>
<div class="message incoming">
<h5>jarvis</h5>
<p>hello ultron</p>
</div>
<div class="message outgoing">
<h5>veronica</h5>
<p>welcome jarvis</p>
</div>
</div>
<div class="user-input">
<input type="text" placeholder="type your message...">
<button>send</button>
</div>
</div>
</div>
</main>
<script>
var window=document.querryselector('.users-window');
function show_hide(){
if(uwindow.style.display=="block"){
uwindow.style.display="none";
}
else{
uwindow.style.display="block";
}
}
</script>
<script src="/socket.io/socket.io.js"></script>
<script src="/js/client.js"></script>
</body>
</html
CHAPTER 3 CSS
3.1 Center Links & Add borders
A common task for CSS is to center text or images. In fact, there are three kinds of centering:
In recent implementations of CSS you can also use features from level 3, which allows centering
absolutely positioned elements:
The most common and (therefore) easiest type of centering is that of lines of text in a paragraph or
in a heading. CSS has the property 'text-align' for that:
P { text-align: center }
H2 { text-align: center }
The lines in this paragraph are all centered between the paragraph's margins, thanks to
the value'center' of the CSS property 'text-align'.
• ul {
li {
text-align: center;
}
3.2 CSS Background Color
The background-color property sets the background color of an element.
The background of an element is the total size of the element, including padding and border (but not
the margin).
Tip: Use a background color and a text color that makes the text easy to read.
• Example:
<!DOCTYPE html>
<html><body>
<p style="background-color:Tomato;">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
</p>
</body></html>
CSS background-image
he background-image property sets one or more background images for an element.
By default, a background-image is placed at the top-left corner of an element, and repeated both
vertically and horizontally.
Tip: The background of an element is the total size of the element, including padding and border (but
not the margin).
• <html><head><style>
• body {
• background-image: url("paper.gif");
•}
• </style></head>
• <body>
• <h1>Hello World!</h1>
• </body></html>
transparent
Example
p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
p.three {
border-style: dotted;
border-color: blue;
• Example:
• p.one {
border-style: solid;
border-color: red;
p.two {
border-style: solid;
border-color: green;
• Example:
• p.one {
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue bottom and yellow left *
•groove - Defines a 3D grooved border. The effect depends on the border-color value
•ridge - Defines a 3D ridged border. The effect depends on the border-color value
•inset - Defines a 3D inset border. The effect depends on the border-color value
•outset - Defines a 3D outset border. The effect depends on the border-color value
The border-style property can have from one to four values (for the top border, right
• <!DOCTYPE html>
• <html><head><style>
• </style></head>
• <body>
• </body></htmlss
Being able to manage the text size is important in web design. However, you should not use font size
adjustments to make paragraphs look like headings, or headings look like paragraphs.
Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs.
Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
Absolute size is useful when the physical size of the output is known
Relative size:
Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px
(16px=1em).
Setting the text size with pixels gives you full control over the text size:
Example
h1 {
font-size: 40px;
h2 {
font-size: 30px;
p{
font-size: 14px;
Tip: If you use pixels, you can still use the zoom tool to resize the entire page.
To allow users to resize the text (in the browser menu), many developers use em instead of pixels.
CSS Font Style
Choosing the right font has a huge impact on how the readers experience a website.
The right font can create a strong identity for your brand.
Using a font that is easy to read is important. The font adds value to your text. It is also important to
choose the correct color and text size for the font.
Serif fonts have a small stroke at the edges of each letter. They create a sense of formality and
elegance.
Sans-serif fonts have clean lines (no small strokes attached). They create a modern and minimalistic
look.
Monospace fonts - here all the letters have the same fixed width. They create a mechanical look.
All the different font names belong to one of the generic font families. S
• <style>
• p.normal {
font-style: normal;
p.italic {
font-style: italic;
p.oblique {
font-style: oblique;
• </style>
3.5 CSS Layout - The position Property
position: static;
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned
according to the normal flow of the page:
Example
div.static {
position: static;
position: relative;
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be
adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by
the element.
Example
div.relative {
position: relative;
left: 30px;
}
ADVERTISEMENT
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which means it always stays in
the same place even if the page is scrolled. The top, right, bottom, and left properties are used to
position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead
of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body,
and moves along with page scrolling.
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
As mentioned in the previous chapter; a block-level element always takes up the full width available
(stretches out to the left and right as far as it can).
Setting the width of a block-level element will prevent it from stretching out to the edges of its
container. Then, you can set the margins to auto, to horizontally center the element within its
container. The element will take up the specified width, and the remaining space will be split equally
between the two margins:
This <div> element has a width of 500px, and margin set to auto.
Note: The problem with the <div> above occurs when the browser window is smaller than the width
of the element. The browser then adds a horizontal scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of small windows. This
is important when making a site usable on small devices:
This <div> element has a max-width of 500px, and margin set to auto.
Tip: Resize the browser window to less than 500px wide, to see the difference between the two divs!
Example
div.ex1 {
width: 500px;
margin: auto;
div.ex2 {
max-width: 500px;
margin: auto;
Note: The problem with the <div> above occurs when the browser window is smaller
than the width of the element. The browser then adds a horizontal scrollbar to the
page.
Using max-width instead, in this situation, will improve the browser's handling of
small windows. This is important when making a site usable on small devices:
EXAMPLE:
<body>
<h2>CSS Max-width</h2>
<br>
</body>
CSS:
<style>
div.ex1 {
div.ex2 {
</style>
3.6 CSS Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
Example
a{
color: hotpink;
In addition, links can be styled differently depending on what state they are in.
Example
/* unvisited link */
a:link {
color: red;
/* visited link */
a:visited {
color: green;
a:hover {
color: hotpink;
/* selected link */
a:active {
color: blue;
When setting the style for several link states, there are some order rules:
• a:link, a:visited {
background-color: #f44336;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
a:hover, a:active {
background-color: red:
With CSS, you have full control over the margins. There are properties for setting the margin for each
side of an element (top, right, bottom, and left).
CSS has properties for specifying the margin for each side of an element:
•margin-top
•margin-right
•margin-bottom
•margin-left
To shorten the code, it is possible to specify all the margin properties in one property.
The margin property is a shorthand property for the following individual margin properties:
•margin-top
•margin-right
•margin-bottom
•margin-left
So, here is how it works:
With CSS, you have full control over the padding. There are properties for setting the padding for
each side of an element (top, right, bottom, and left).
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
inherit - specifies that the padding should be inherited from the parent element
• div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
• div {
The height and width properties do not include padding, borders, or margins. It sets the
height/width of the area inside the padding, border, and margin of the element.
The height and width properties may have the following values:
auto - This is default. The browser calculates the height and width
The height and width properties may have the following values:
•auto - This is default. The browser calculates the height and width
•div {
height: 200px;
width: 50%;
background-color: powderblue;
Look at CSS Color Values for a complete list of possible color values.
The default text color for a page is defined in the body selector.
Example
body {
color: blue;
h1 {
color: green;
• Example:
This text is styled with some of the text formatting properties. The heading uses the text-align, text-
transform, and color properties. The paragraph is indented, aligned, and the space between
characters is specified. The underline is removed from this colored "Try it Yourself" link.
CSS Syntax
text-decoration-line (required)
text-decoration-color
text-decoration-style
text-decoration-thickness
• h2 {
text-decoration: overline;
h3 {
text-decoration: line-through;
h4 {
text-decoration: underline;
• The value text-decoration: none; is often used to remove underlines from links:
•a{
text-decoration: none;
The cursor property specifies the mouse cursor to be displayed when pointing over an element.
Example
h1 {
color: navy;
margin-left: 20px;
• Each HTML page must include a reference to the external style sheet file
• Example:
• <!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Font Weight
The font-weight property specifies the weight of a font:
p.normal {
font-weight: normal;
p.thick {
font-weight: bold;
Font Variant
The font-variant property specifies whether or not a text should be displayed in a small-caps font.
p.normal {
font-variant: normal;
p.small {
font-variant: small-caps;
Code of CSS
*{
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
main {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
width: 80%;
overflow: hidden;
border-radius: 10px;
}
main header {
background-color: #2791cf;
color: #ffffff;
padding: 7px 20px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
main div.container {
background-color: #7E878c;
padding: 20px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: start;
-ms-flex-pack: start;
justify-content: flex-start;
-webkit-box-align: stretched;
-ms-flex-align: stretched;
align-items: stretched;
}
border-radius: 50px;
}
width: 100%;
border-radius: 10px;
background-color: #3f3f3f;
}
background-color: #2791cf;
border-radius: 50px 10px 10px 50px;
text-align: right;
}
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
-webkit-box-align: stretched;
-ms-flex-align: stretched;
align-items: stretched;
padding: 20px;
}
height: -webkit-max-content;
height: -moz-max-content;
height: max-content;
z-index: 11;
-webkit-box-shadow: 0 15px 20px -20px black;
box-shadow: 0 15px 20px -20px black;
padding-bottom: 0;
display: none;
}
main div.container div.chat-window div.chats div.message {
max-width: 300px;
}
}
/*# sourceMappingURL=index.css.map */
CHAPTER 4 JAVA SCRIPT
To do so, you have to find the elements first. There are several ways to do
this:
Example
const element = document.getElementById("intro");
If the element is found, the method will return the element as an object (in
element).
Example
const element = document.getElementsByTagName("p");
This example finds the element with id="main", and then finds all <p> elements
inside "main":
Example
const x = document.getElementById("main");
const y = x.getElementsByTagName("p");
Example
const x = document.getElementsByClassName("intro");
If you want to find all HTML elements that match a specified CSS selector (id,
class names, types, attributes, values of attributes, etc), use
the querySelectorAll() method.
Example
const x = document.querySelectorAll("p.intro");
Example
const x = document.forms["frm1"];
let text = "";
for (let i = 0; i < x.length; i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
The following HTML objects (and object collections) are also accessible:
• document.anchors
• document.body
• document.documentElement
• document.embeds
• document.forms
• document.head
• document.images
• document.links
• document.scripts
• document.title
Exercise:
Use the getElementById method to find the <p> element, and change its text to
"Hello".
<p id="demo"></p>
<script>
= "Hello";
</script>
document.getElementById("demo");
document.getElementById("demo").style.color = "red";
Note
Any id should be unique, but:
If two or more elements with the same id exist, getElementById() returns the
first.
Syntax
document.getElementById(elementID)
Parameters
Parameter Description
id Required.
The id value of an element.
Return Value
Type Description
Object The element with the specified id.
null if the element does not exist.
Return Value
Type Description
Object A DOMTokenList.
A list of the class names of an element.
Note
The classList property is read-only, but you can use the methods listed
below, to add, toggle or remove CSS classes from the list:
More Examples
Add multiple classes to the an element:
element.classList.add("myStyle", "anotherClass", "thirdClass");
let x = element.classList.contains("myStyle");
if (element.classList.contains("mystyle")) {
element.classList.remove("anotherClass");
}
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
Create a sticky navigation bar:
// Add the sticky class to the navbar when you reach its scroll
position
// Remove it when you leave the scroll position
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
JavaScript Let
The let keyword was introduced in ES6 (2015).
Cannot be Redeclared
Variables defined with let can not be redeclared.
let x = 0;
With var you can:
var x = 0;
Block Scope
Before ES6 (2015), JavaScript had Global Scope and Function Scope.
ES6 introduced two important new JavaScript keywords: let and const.
Example
{
let x = 2;
}
// x can NOT be used here
Variables declared with the var keyword can NOT have block scope.
Variables declared inside a { } block can be accessed from outside the block.
Example
{
var x = 2;
}
// x CAN be used here
Redeclaring Variables
Redeclaring a variable using the var keyword can impose problems.
Redeclaring a variable inside a block will also redeclare the variable outside
the block:
Example
var x = 10;
// Here x is 10
{
var x = 2;
// Here x is 2
}
// Here x is 2
Redeclaring a variable using the let keyword can solve this problem.
Redeclaring a variable inside a block will not redeclare the variable outside
the block:
Example
let x = 10;
// Here x is 10
{
let x = 2;
// Here x is 2
}
// Here x is 10
Browser Support
The let keyword is not fully supported in Internet Explorer 11 or earlier.
The following table defines the first browser versions with full support for
the let keyword:
Mar, 2016 Jul, 2015 Jan, 2015 Sep, 2017 Mar, 2016
Let Hoisting
Variables defined with var are hoisted to the top and can be initialized at any
time.
Example
This is OK:
carName = "Volvo";
var carName;
If you want to learn more about hoisting, study the chapter JavaScript
Hoisting.
Variables defined with let are also hoisted to the top of the block, but not
initialized.
Example
carName = "Saab";
let carName = "Volvo";
Example
#myProgress {
width: 100%;
background-color: grey;
}
#myBar {
width: 1%;
height: 30px;
background-color: green;
}
Example
var i = 0;
function move() {
if (i == 0) {
i = 1;
var elem = document.getElementById("myBar");
var width = 1;
var id = setInterval(frame, 10);
function frame() {
if (width >= 100) {
clearInterval(id);
i = 0;
} else {
width++;
elem.style.width = width + "%";
}
}
}
}
4.5 Add Labels
If you want to add labels to indicate how far the user is in the process, add a
new element inside (or outside) the progress bar:
Example
<div id="myProgress">
<div id="myBar">10%</div>
</div>
Example
#myBar {
width: 10%;
height: 30px;
background-color: #04AA6D;
text-align: center; /* To center it horizontally (if you want) */
line-height: 30px; /* To center it vertically */
color: white;
}
If you want to dynamically update the text inside the label to the same value
of the width of the progress bar, add the following:
Example
var i = 0;
function move() {
if (i == 0) {
i = 1;
var elem = document.getElementById("myBar");
var width = 10;
var id = setInterval(frame, 10);
function frame() {
if (width >= 100) {
clearInterval
(id);i = 0;
}
els
e {
wid
th+
+;
elem.style.width = width +
"%";elem.innerHTML = width
+ "%";
}
}
}
}
Code of JS
Client side js code
const socket=io();
var username;
var chats=document.querySelector(".chats");
var users_list=document.querySelector(".users_list");
var users_count=document.querySelector(".users_count");
var msg_send=document.querySelector(".user_send");
var user_msg=document.querySelector(".user_msg");
do{
username=prompt("enter your name:");
}while(username);
socket.emit("new-user-joined",username);
socket.on('user_connected',(socket_name)=>{
userJoinLeft(socket_name,'joined')
});
function userJoinLeft(name,ststus){
let div=document.createElement("div");
div.classList.add('user_join');
let content='<p><b>${name}</b> ${ststus} the chat</p>';
div.innerHTML=content;
chats.appendChild(div);
}
socket.on('user-disconnected',(user)=>{
userJoinLeft(user,'left');
});
socket.on('user-list',(users)=>{
users_list.innerHTML="";
users_arr=Object.values(users);
for(i=o;i<users_arr.length;i++){
let p=document.createElement('p');
p.innerText=users_arr[i];
users_list.appendChild(p);
}
users_count.innerHTML=users_arr.length;
});
/* for sending message */
msg_send.addEventListener('click',()=>{
let data={
user: username,
msg: user_msg.value
};
if(user_msg.value!=''){
appendMessage(data,'outgoing');
socket.emit('message',data);
user_msg.value='';
}
});
function appendMessage(data,status){
let div=document.createElement('div');
div.classList.add('message',status);
let content='
<h5>${data.user}</h5>
<p>${data.msg}</p>
';
div.innerHTML=content;
chats.appendChild(div);
};
const app=express();
const server=http.createServer(app);
const port=process.env.PORT || 3000;
app.use(express.static(__dirname+'/public'));
app.get('/',(req,res)=>{
res.sendFile(__dirname+'/index.html');
});
/*socket.io setup */
const io=require("socket.io")(server);
var users={};
io.on("connection",(socket)=>{
socket.on("new-user-joined",(username)=>{
users[socket.id]=username;
socket.broadcast.emit('user-connected',username);
io.emit("user-list",users);
});
socket.on("disconnect3ed"()=>{
socket.broadcat.emit('user-disconnected',user=users[socket.id]);
delete users[socket.id];
io.emit("user-list",users);
});
socket.on('message',(data)=>{
socket.broadcast.emit("message",{user: data.user,msg: data.msg})
});
});
CHAPTER 5: NODE JS
5.1 NODEMON:
nodemon is a tool that helps develop Node.js based applications
by automatically restarting the node application when file changes
in the directory are detected.
nodemon does not require any additional changes to your code or
method of development. nodemon is a replacement wrapper
for node. To use nodemon, replace the word node on the
command line when executing your script.
Installation
Either through cloning with git or by using npm (the recommended way):
npm install -g nodemon # or using yarn: yarn global add nodemon
With a local installation, nodemon will not be available in your system path or you can't
use it directly from the command line. Instead, the local installation of nodemon can be
run by calling it from within an npm script (such as npm start) or using npx nodemon.
Usage
nodemon wraps your application, so you can pass all the arguments you would normally
pass to your app:
nodemon [your node app]
For CLI options, use the -h (or --help) argument:
nodemon -h
Using nodemon is simple, if my application accepted a host and port as the arguments, I
would start it as so:
nodemon ./server.js localhost 8080
Any output from this script is prefixed with [nodemon], otherwise all output from your
application, errors included, will be echoed out as expected.
You can also pass the inspect flag to node through the command line as you would
normally:
nodemon --inspect ./server.js 80
If you have a package.json file for your app, you can omit the main script entirely and
nodemon will read the package.json for the main property and use that value as the app
(ref).
nodemon will also search for the scripts.start property in package.json (as of nodemon
1.1.x).
Automatic re-running
nodemon was originally written to restart hanging processes such as web servers, but
now supports apps that cleanly exit. If your script exits cleanly, nodemon will continue to
monitor the directory (or directories) and restart the script if there are any changes.
Manual restarting
Whilst nodemon is running, if you need to manually restart your application, instead of
stopping and restart nodemon, you can type rs with a carriage return, and nodemon will
restart your process.
Config files
nodemon supports local and global configuration files. These are usually
named nodemon.json and can be located in the current working directory or in your home
directory. An alternative local configuration file can be specified with the --config
<file> option.
The specificity is as follows, so that a command line argument will always override the
config file settings:
"verbose": true,
"execMap": {
"rb": "ruby",
The above nodemon.json file might be my global config so that I have support for ruby files
and processing files, and I can run nodemon demo.pde and nodemon will automatically
know how to run the script even though out of the box support for processing scripts.
package.json
If you want to keep all your package configurations in one place, nodemon supports
using package.json for configuration. Specify the config in the same format as you would
for a config file but under nodemonConfig in the package.json file, for example, take the
following package.json:
{
"name": "nodemon",
"homepage": "http://nodemon.io",
"...": "... other standard package.json values",
"nodemonConfig": {
"delay": 2500
This section needs better documentation, but for now you can also see nodemon --help
config (also here).
Now nodemon will run app.py with python in verbose mode (note that if you're not
passing args to the exec program, you don't need the quotes), and look for new or
modified files with the .py extension.
Default executables
Using the nodemon.json config file, you can define your own default executables using
the execMap property. This is particularly useful if you're working with a language that isn't
supported by default by nodemon.
To add support for nodemon to know about the .pl extension (for Perl),
the nodemon.json file would add:
{
"execMap": {
"pl": "perl"
Now running the following, nodemon will know to use perl as the executable:
nodemon script.pl
Now nodemon will only restart if there are changes in the ./app or ./libs directory. By
default nodemon will traverse sub-directories, so there's no need in explicitly including
sub-directories.
Nodemon also supports unix globbing, e.g --watch './lib/*'. The globbing pattern must
be quoted.
nodemon -e js,pug
Now nodemon will restart on any changes to files in the directory (or subdirectories) with
the extensions .js, .pug.
Ignoring files
By default, nodemon will only restart when a .js JavaScript file changes. In some cases
you will want to ignore some specific files, directories or file patterns, to prevent
nodemon from prematurely restarting your application.
Important the ignore rules are patterns matched to the full absolute path, and this
determines how many files are monitored. If using a wild card glob pattern, it needs to be
used as ** or omitted entirely. For example, nodemon --ignore '**/test/**' will work,
whereas --ignore '*/test/*' will not.
Though this should be a last resort as it will poll every file it can find.
Delaying restarting
In some situations, you may want to wait until a number of files have changed. The
timeout before checking for new file changes is 1 second. If you're uploading a number of
files and it's taking some number of seconds, this could cause your app to restart
multiple times unnecessarily.
The delay figure is number of seconds (or milliseconds, if specified) to delay before
restarting. So nodemon will only restart your app the given number of seconds after
the last file change.
If you are setting this value in nodemon.json, the value will always be interpreted in
milliseconds. E.g., the following are equivalent:
nodemon --delay 2.5
"delay": 2500
reloadSomeConfiguration();
})
Please note that nodemon will send this signal to every process in the process tree.
If you are using cluster, then each workers (as well as the master) will receive the signal.
If you wish to terminate all workers on receiving a SIGHUP, a common pattern is to catch
the SIGHUP in the master, and forward SIGTERM to all workers, while ensuring that all
workers ignore SIGHUP.
if (cluster.isMaster) {
process.on("SIGHUP", function () {
worker.process.kill("SIGTERM");
});
} else {
The following example will listen once for the SIGUSR2 signal (used by nodemon to
restart), run the clean up process and then kill itself for nodemon to continue control:
process.once('SIGUSR2', function () {
gracefulShutdown(function () {
process.kill(process.pid, 'SIGUSR2');
});
});
Note that the process.kill is only called once your shutdown jobs are complete. Hat tip
to Benjie Gillam for writing this technique up.
"events": {
A full list of available events is listed on the event states wiki. Note that you can bind to
both states and messages.
script: ...,
this.stdout.pipe(fs.createWriteStream('output.txt'));
this.stderr.pipe(fs.createWriteStream('err.txt'));
});
5.2 SOCKET.IO
Sockets.IO :
Earlier, websites used to reload every-time a resource was requested. This
introduced unnecessary delays which increased average wait time. Often users
had to wait for minutes to fetch a particular page or file. Real-time
applications(Instant messenger, Online gaming, push notification etc), on the
other hand, are those applications which run within a given time-slot such that
user is presented with immediate and up-to-date copy of the resource. Latency
in these applications is kept as low as possible to give smooth and consistent
user experience. Socket.IO is one such JavaScript library that programmers use
in developing real-time “Web Applications”.
Installing:
Required for Installation of Sockets.IO
• Node Installed
• npm (node package manager) installed
As mentioned earlier, there are two different libraries to include in a project.
• Server:
To install it in the Node.js project, run the following command,
$ npm install --save socket.io
• JavaScript Client:
A standalone build of the client is exposed by default by the server
at /socket.io/socket.io.js. Otherwise, it can also be served from any of the
CDN providers.
To use it from Node.js, install it via this command,
$ npm install --save socket.io-client
OUTPUT: