Chat Server In NodeJS
Posted By : Karan Bhardwaj | 10-Feb-2015
Basic Description:
Node js is an Open Source, Cross Platform Run Time Environment For Server Side And Networking Applications.Node Js is truley based on JavaScript.Node Js is PowerFull Run Time Environment to Develop web-application,websites,Network Applications etc. Node Js is a latest,PowerFull and advanced Technology to develop all types of web applications on server side and all types of networking applications. Node Js Uses Google V8 Javascript Engine to execute Code.
Steps To Create Chat Application
npm install express npm install jade npm install socket.io
var express = require("express"); var app = express(); var port = 8000; app.set('views', __dirname + '/tpl'); app.set('view engine', "jade"); app.engine('jade', require('jade').__express); app.get("/", function(req, res){ res.render("page"); //res.send("It works!"); }); app.use(express.static(__dirname + '/public')); var io = require('socket.io').listen(app.listen(port)); io.sockets.on('connection', function (socket) { socket.emit('message', { message: 'welcome to the chat' }); socket.on('send', function (data) { io.sockets.emit('message', data); }); }); console.log("Listening on port " + port);
In above we have created a application and define its port number through which we can access our application. using “.set” we defined the directory name and path of the jade file and defined the view engine for our project which is jade. we are using jade framework instead of simple or core html. using jade framework we can easily create a very good UI for our application. jade is only intended to produce XML Like Documents(HTML, RSS etc.). using socket.io we are able to make networking function with in our application. In this we are able make connections from different sockets and from different networks and we are getting able to send messages and display it into our screens.
doctype html html(lang="en") head title= "Real time web chat" script(src='/chat.js'). script(src='/socket.io/socket.io.js'). body #content(style='width: 500px; height: 300px; margin: 0 0 20px 0; border: solid 1px #999; overflow-y: scroll;') .controls | Name: input#name(style='width:350px;') br input#field(style='width:350px;') input#send(type='button', value='send')
window.onload = function() { var messages = []; var socket = io.connect('http://192.168.1.173:8000'); var field = document.getElementById("field"); var sendButton = document.getElementById("send"); var content = document.getElementById("content"); var name = document.getElementById("name"); socket.on('message', function (data) { if(data.message) { messages.push(data); var html = ''; for(var i=0; i
- First Of all you Need to be Installed Node Js On your System. For Downloading node Js please Follow this link http://nodejs.org/dist/v0.12.0/node-v0.12.0.pkg
- After Installing You have to download 3 packages as mentioned bellow.
- Express
- Jade
- Socket.io
- Create a directory in which you want to create your project and in that director through terminal install the above packages by following the below commands in terminal.
- Create a file index.js in your main project folder and write down the bellow code lines in your index.js file.
- Create a directory named as tpl in your main project folder. Now put the below mentioned code in page.jade file in tpl directory. it’s html,css and javascript code.Format of this code is according to the jade framework. for further refrence Please look forward for the jade template framework website.
- Create a directory named as public in your main directory of the project. in that folder create a file containing the main part of our chat application. in this we are defining the ip address and port number for our chat application and created some functions through which we get and put the messages into there specified areas or div and defined the functioning of send button
To run this project move to that folder in which your project is located and run the below command.
node index.js
After that open your bowser and go to that ip address which you defining in your project. like : http://192.168.1.173:8000.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Karan Bhardwaj
Karan is expert in JavaScript, JQuery, Phonegap, Java, Groovy and Grails, SpringBoot, Kurento, MongoDB, NodeJs, Wowza, FFmpeg and Kaltura.