How To Make A Live Chat In JavaScript With Source Code

[SOLVED] How To Make A Live Chat In JavaScript With Source Code

This tutorial How To Make A Live Chat In JavaScript is developed using PHP, JavaScript, CSS, and Bootstrap.

This Live Chat JavaScript Code is a simple web-based application that creates messages through the other users present in the application.

It also includes a login and logout system using Ajax features. Multiple users can start conversations, share messages at the same time. There’s only User side, so all the functions are to be performed from the user’s side.

The Live Chat Tutorial Design of this project is very simple and similar to-other public chatrooms so that the user won’t find difficult to use. 

To run this project you must have installed virtual server i.e XAMPP on your pc (for Windows).

This Live Chat JavaScript also includes a downloadable Live Chat JavaScript Code for free, just find the downloadable source code below and click to start downloading.

I have here a suggested list of Best JavaScript Projects with Source Code Free to download and I’m sure this can help you to improve your skills in JavaScript programming and web development as a whole.

To start creating on How To Make A Live Chat In JavaScript, makes sure that you have any platform in creating a JavaScriptCSS,  bootstrap, and HTML installed in your computer, in my case I will use Sublime Text.

Steps on How To Make A Live Chat In JavaScript With Source Code

How To Make A Live Chat In JavaScript With Source Code

  • Step 1: Create a folder from “htdocs”.

    First create a folder from the “htdocs” and named it.
    live chat create folder

  • Step 2: Paste the Source Code.

    Second after you create a folder from “htdocs” paste the source code you’ve download below.
    live chat project

  • Step 3: Open XAMPP Control Panel.

    Third open the “XAMMP” control panel and click the apache “start”.
    live chat xampp

  • Step 4: Run the project.

    Fourth you can run now the project.
    live chat interface

The Code Given Below Is For The Index File

<?php
session_start ();
function loginForm() {
    echo '
	<div class="form-group">
		<div id="loginform">
			<form action="index.php" method="post">
			<h1>Live Chat</h1><hr/>
				<label for="name">Please Enter Your Name To Proceed..</label>
				<input type="text" name="name" id="name" class="form-control" placeholder="Enter Your Name"/>
				<input type="submit" class="btn btn-default" name="enter" id="enter" value="Enter" />
			</form>
		</div>
	</div>
   ';
}
 
if (isset ( $_POST ['enter'] )) {
    if ($_POST ['name'] != "") {
        $_SESSION ['name'] = stripslashes ( htmlspecialchars ( $_POST ['name'] ) );
        $cb = fopen ( "log.html", 'a' );
        fwrite ( $cb, "<div class='msgln'><i>User " . $_SESSION ['name'] . " has joined the chat session.</i><br></div>" );
        fclose ( $cb );
    } else {
        echo '<span class="error">Please Enter a Name</span>';
    }
}
 
if (isset ( $_GET ['logout'] )) {
    $cb = fopen ( "log.html", 'a' );
    fwrite ( $cb, "<div class='msgln'><i>User " . $_SESSION ['name'] . " has left the chat session.</i><br></div>" );
    fclose ( $cb );
    session_destroy ();
    header ( "Location: index.php" );
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Simple Live Chat Using PHP and Javascript</title>
	<link rel="stylesheet" href="css/style.css">
	<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
	<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
<?php
	if (! isset ( $_SESSION ['name'] )) {
	loginForm ();
	} else {
?>
<div id="wrapper">
	<div id="menu">
	<h1>Live Chat!</h1><hr/>
		<p class="welcome"><b>HI - <a><?php echo $_SESSION['name']; ?></a></b></p>
		<p class="logout"><a id="exit" href="#" class="btn btn-default">Exit Live Chat</a></p>
	<div style="clear: both"></div>
	</div>
	<div id="chatbox">
	<?php
		if (file_exists ( "log.html" ) && filesize ( "log.html" ) > 0) {
		$handle = fopen ( "log.html", "r" );
		$contents = fread ( $handle, filesize ( "log.html" ) );
		fclose ( $handle );

		echo $contents;
		}
	?>
	</div>
<form name="message" action="">
	<input name="usermsg" class="form-control" type="text" id="usermsg" placeholder="Create A Message" />
	<input name="submitmsg" class="btn btn-default" type="submit" id="submitmsg" value="Send" />
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
});
$(document).ready(function(){
    $("#exit").click(function(){
        var exit = confirm("Are You Sure You Want To Leave This Page?");
        if(exit==true){window.location = 'index.php?logout=true';}     
    });
});
$("#submitmsg").click(function(){
        var clientmsg = $("#usermsg").val();
        $.post("post.php", {text: clientmsg});             
        $("#usermsg").attr("value", "");
        loadLog;
    return false;
});
function loadLog(){    
    var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
    $.ajax({
        url: "log.html",
        cache: false,
        success: function(html){       
            $("#chatbox").html(html);       
            var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
            if(newscrollHeight > oldscrollHeight){
                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal');
            }              
        },
    });
}
setInterval (loadLog, 2500);
</script>
<?php
}
?>
</body>
</html>

In this module which is the index of the system.

The Code Given Below Is For The CSS File

body {
    font:12px arial;
    color: #222;
    text-align:center;
    padding:35px; }
  
form, p, span {
    margin:0;
    padding:0; }
  
input { font:12px arial; }
  
a {
    color:#0000FF;
    text-decoration:none; }
  
    a:hover { text-decoration:underline; }
  
#wrapper, #loginform {
    margin:0 auto;
    padding-bottom:25px;
    background:violet;
    width:445px;
    border:1px solid #ACD8F0;
    border-radius: 5px;	}
  
#loginform { padding-top:18px; }
  
#loginform p { margin: 5px; }
  
#chatbox {
    text-align:left;
    margin:0 auto;
    margin-bottom:25px;
    padding:10px;
    background:#fff;
    height:270px;
    width:430px;
    border:1px solid #ACD8F0;
    overflow:auto; }

#submit { width: 60px; }
  
.error { color: #ff0000; }
  
#menu { padding:12.5px 25px 12.5px 25px; }
  
.welcome { float:left; }
  
.logout { float:right; }
  
.msgln { margin:0 0 2px 0; }

In this module which is the css file or the style of the system.

ABOUT PROJECTPROJECT DETAILS
Project Name :How To Make A Live Chat In JavaScript
Project Platform :JavaScript
Programming Language Used:php,javascript,html,css
Developer Name :itsourcecode.com
IDE Tool (Recommended):Sublime
Project Type :Web Application
Database:None
Upload Date:November 20, 2020

Downloadable Source Code

Summary

This JavaScript Project With Source Code is simply in HTMLCSS, and JavaScript.

Talking about the project, A user has to enter his/her name before entering the chatroom.

After that, the user can easily share messages present in the chatroom. This project includes a lot of JavaScript for making the functioning of the project.

Related Articles

Inquiries

If you have any questions or suggestions about How To Make A Live Chat In JavaScript , please feel free to leave a comment below.

Leave a Comment