Hello, Habr. Console chat is a great thing, but for the front-end, what if you want the same, but for the backend. If yes, then this article is for you. But which tool is often used in the backend? That's right ssh, so I represent sshchat.
How will it look like
Somewhere on the server, the program on the node is spinning.
As soon as someone wants to connect to the chat, he enters:
ssh server -p 8022
After that, the system asks for a password and verifies it with the password in a special file. If the password matches, then connect to the chat (the user receives 100 previous messages and everyone else sees that he is connected).
Then he receives the messages of others, and can write his own.
Here with messages more interesting:
@box{@color(red){Red text in box}}
Send red text in box.
Let's get started
To work with ssh we will use https://www.npmjs.com/package/ssh2 .
For formatting, we use chalk and boxen.
So install them:
npm i ssh2 chalk boxen
Now the code itself is one of the most important parts of this message parser ( GitHub ):
Formatting ( GitHub ):
const chalk = require('chalk'); const { parseAndExecute } = require('./parserExec')
Methods for sending a message to all users and saving 100 messages ( GitHub ):
let listeners = [];
Lobby, server creation and authorization ( GitHub ):
const { Server } = require('ssh2'); const { readFileSync } = require('fs'); const hostKey = readFileSync('./ssh');
Various methods ( GitHub ):
const { createInterface } = require('readline'); module.exports.getStream = function(client, onStream, onEnd){ client
Now combine ( GitHub ):
const { createServer, setConnectCallback } = require('./lobby'); const { getStream, getCommunicator } = require('./utils'); const { addListener, delListener, broadcast, getCache } = require('./broadcaster'); const { format, getNick } = require('./format');
And the final step is an example server:
const chat = require('.') chat({})
Also, users and their passwords are described in the users.json file.
findings
This is how you can write not the easiest chat in ssh.
For such a chat, the client does not need to write, it has the design capabilities, and anyone can deploy it.
What else can be done:
- Add the ability to create your own design features
- Add markdown support
- Add bot support
- Sending files by scp
- Increase password security (hash and salt)
Final repository