d.Hello all.
Today in this post, I am going to explain about how to use a "net" module in node.js and build a basic chat server.(i.e a TCP chat server)
For this, you need to use either telnet / netcat(nc).(I have tried it on netcat, simply google "netcat download")
(Dont worry about the script for now, i will upload it, just go through the tutorial to understand the concept)
So, lets begin,
first of all to use node "net" module , we need to import it in out script.
var net = require("net")
This will import the net module. Now moving ahead, we need to create a server. This is done by createServer function(on simply Server function).
var server = net.createServer()
createServer has a callback whenever a client connects to a server.
i.e
var server = net.createServer(function(socket){
});
The code inside the createServer() function is callback(Hope you have know about the callbacks / event driven programming). This gets called whenever, a client gets connected to a server.
Now there are various events that can occur but we will focus mainly on "data" , "close" events.
socket.on('data',function(data){
// this gets called whenever a clients send the data
});
socket.on('close',function(){
// this gets called whenever a socket is closed i.e. connection is terminate
});
So a basic TCP server looks like,
tcp_server.js
var net = require('net');
var s = net.createServer(function(socket){
socket.on('data',function(d){
console.log("data is received");
});
socket.on('close',function(){
console.log("socket closed");
});
socket.on("error",function(err){
console.log("An error occurs");
});
});
s.listen(1234);
we have create a server and binds it port 1234.
To use it, run the server fie( node tcp_server.js)
To create clients,, nc localhost 1234
just type anything and hit enter
You will see the "Data is received" on server [ as "data" events occurs ]
Then close the client window.
You will see "socket closed" on server [ as close event occurs]
and that's it.
if the above idea is clear, this is enough to create a basic chat server.
Just create an array to hold the participants, add the new members to the array as the new clients joins, removes them from array as they gets disconnected.
Code for a Very Basic TCP Chat Server.
var net = require("net");
var participant = []; // array of participanticpants
var current = -1;
var total_participants = 0;
var server = net.createServer(function(socket){
participant.push(socket);
total_participants++;
for(var i=0;i<participant.length;i++){
if(participant[i] == socket) continue;
participant[i].write("A new particpant joined the chat\n");
participant[i].write("Now there are total " +total_participants+" members\n");
}
socket.on('data',function(data){
for(var i=0;i<participant.length;i++){
if(participant[i] == socket) {
current = i;
continue;
}
participant[i].write(i+" says : "+data);
}
});
socket.on('close',function(){
var i = participant.indexOf(socket);
participant.splice(i,1);
total_participants--;
for(var i=0;i<participant.length;i++){
participant[i].write(i+" left the chat\n");
participant[i].write("Now there are total " +total_participants+" members\n");
}
});
socket.on("error",function(err){
//console.log("error cooured-->"+err)
});
// socket.close();
});
server.listen(1234);
Hope this will helps.
Just leave a comment as a feedback if you like the post.
You can also check our Microservices post
Introduction to Microservices
Today in this post, I am going to explain about how to use a "net" module in node.js and build a basic chat server.(i.e a TCP chat server)
For this, you need to use either telnet / netcat(nc).(I have tried it on netcat, simply google "netcat download")
(Dont worry about the script for now, i will upload it, just go through the tutorial to understand the concept)
So, lets begin,
first of all to use node "net" module , we need to import it in out script.
var net = require("net")
This will import the net module. Now moving ahead, we need to create a server. This is done by createServer function(on simply Server function).
var server = net.createServer()
createServer has a callback whenever a client connects to a server.
i.e
var server = net.createServer(function(socket){
});
The code inside the createServer() function is callback(Hope you have know about the callbacks / event driven programming). This gets called whenever, a client gets connected to a server.
Now there are various events that can occur but we will focus mainly on "data" , "close" events.
socket.on('data',function(data){
// this gets called whenever a clients send the data
});
socket.on('close',function(){
// this gets called whenever a socket is closed i.e. connection is terminate
});
So a basic TCP server looks like,
tcp_server.js
var net = require('net');
var s = net.createServer(function(socket){
socket.on('data',function(d){
console.log("data is received");
});
socket.on('close',function(){
console.log("socket closed");
});
socket.on("error",function(err){
console.log("An error occurs");
});
});
s.listen(1234);
we have create a server and binds it port 1234.
To use it, run the server fie( node tcp_server.js)
To create clients,, nc localhost 1234
just type anything and hit enter
You will see the "Data is received" on server [ as "data" events occurs ]
Then close the client window.
You will see "socket closed" on server [ as close event occurs]
and that's it.
if the above idea is clear, this is enough to create a basic chat server.
Just create an array to hold the participants, add the new members to the array as the new clients joins, removes them from array as they gets disconnected.
Code for a Very Basic TCP Chat Server.
var net = require("net");
var participant = []; // array of participanticpants
var current = -1;
var total_participants = 0;
var server = net.createServer(function(socket){
participant.push(socket);
total_participants++;
for(var i=0;i<participant.length;i++){
if(participant[i] == socket) continue;
participant[i].write("A new particpant joined the chat\n");
participant[i].write("Now there are total " +total_participants+" members\n");
}
socket.on('data',function(data){
for(var i=0;i<participant.length;i++){
if(participant[i] == socket) {
current = i;
continue;
}
participant[i].write(i+" says : "+data);
}
});
socket.on('close',function(){
var i = participant.indexOf(socket);
participant.splice(i,1);
total_participants--;
for(var i=0;i<participant.length;i++){
participant[i].write(i+" left the chat\n");
participant[i].write("Now there are total " +total_participants+" members\n");
}
});
socket.on("error",function(err){
//console.log("error cooured-->"+err)
});
// socket.close();
});
server.listen(1234);
Hope this will helps.
Just leave a comment as a feedback if you like the post.
You can also check our Microservices post
Introduction to Microservices
Nice tutorial ..
ReplyDeletekeep sharing ..
Thanx