A simple TCP chat-server that accepts clients that connect to it over TCP. You can use netcat
or ncat
to connect to the server.
This chat server can do 5 things:
- Broadcast messages from any client to all other connected clients (like how you would expect a chat room to work).
- Upon connecting prompt the client for their nickname.
- This nickname is case-sensitive and must be unique on the server.
- If the nickname is already taken the server should tell the user to choose a new nickname.
- Upon entering a valid nickname, the server should:
- send the last 10 lines of chat in the chat room,
- broadcast that the user has connected to the other clients
- send a list of users that are currently connected to the just-connected client.
- Upon disconnecting, the server should:
- broadcast that the user has disconnected.
- If the user is "@mentioned", meaning the message contains @theirnickname, a BEL ('\a') character should be sent to the client that is connected with that nickname.
Example Interaction:
< Welcome to my chat server! What is your nickname?
Peter
< You are connected with 3 other users: [Tammy, Betty, King]
< Retrieving lastest 3 messages in the chat server ...
< [03:36:08] <Tammy> Hi guys
< [03:36:13] <Tammy> How are you doing today
< [03:36:24] <King> Great!!
< [03:38:50] <Betty> Nice to meet you all
Nice to meet you too!
< [03:40:24] <Tammy> Glad to be here
- You have Node and git installed
- You have either
netcat
orncat
installed
- Running the following to install necessary dependencies for my-chat-app
npm install
- Done
netcat
or nc
in pre-installed in Terminal.
netcat
or nc
might have been pre-installed in Terminal. If not, run apt update && apt install -y netcat
.
It is recommended to install ncat
from https://nmap.org/ncat/. After downloading the zip file, follow the steps below to complete the installation
- Unzip the downloaded ncap zip file.
- Go to directory with ncat.exe.
- Open start menu and search for "Edit the System environment variables" -> Environment Variables.
- For "System variables", scroll down to look for "Path".
- Double click "Path", click "New" to add the directory where ncat.exe is placed or move ncat.exe to one of the path in "Path".
- Run the following to start my-chat-app
npm run prod
- Done
- Connect to my-chat-app with host name "localhost" and port "3000"
netcat localhost 3000
- Open Terminal,
cd
to the directory of ncat.exe if you haven't placed it to one of the environmental variables - Connect to my-chat-app
ncat localhost 3000
I created Dockerfile and added scripts in package.json to allow building docker image for my-chat-app and also run it in Docker container. If you have Docker installed, please try out this section!
- Build docker image with the Dockerfile
npm run docker-build
- Done
npm run docker-start
npm run docker-start-win
npm run docker-start-bkg
npm run docker-start-bkg-win
- This will remove the my-chat-app container completely
npm run docker-stop
- Done
- Build docker image for netcat client using Dockerfile-client
npm run docker-client-build
- Done
npm run docker-client-start
Cmd line: <local ip address or ip address of docker container of my-chat-app> 3000
To find out docker container ip address of my-chat-app, if you run npm run docker-start
or npm run docker-start-win
, the IP address will be shown like below
> my-chat-app@1.0.0 start
> nodemon server.js
[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): _._
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server is up and can be connected through 172.17.0.2:3000
By default, logs are saved in log
directory.
- Install dev dependencies
npm install --dev
- Start testing using jest
npm run test
- There should be 8 tests to be passed.
- If all tests are passed, done
- if you haven't installed any dependencies
npm install
- Install dev dependencies
npm install --dev
- Start development with nodemon
npm run start
MIT License
Copyright (c) 2020 gareth0712
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.