A simple telnet app to chat with a server. It uses
expect to chat and wait for an expected response.
There is also an
exec method to send and expect in sequence.
This module allows you to chat programatically to any server such as a POP3 server or an SMTP server that uses a known protocol that can be sent to and expected. It uses native Promises and is pipeline safe: you can send and expect from multiple async calls and the data will arrive back in the right order.
npm install chat2
static Chat2 = ;static chat = 'localhost' 110 5000;chat
const chat2 = host port timeout;
hostname or IP address
a numeric port, defaulting to 23 if not provided
a timeout in milliseconds, defaulting to no timeout
This sets up the connection to the server, sets encoding to utf-8 and sets a keepalive. It mus be called before any other methods.
Called with a regular expression, it returns a promise. The promise resolves when the regular expression is matched, returning the full line that matched.
Sends a message to the server, returning a promise. The promise resolves with the message sent.
send followed by
expect. It returns a promise
which is resolved to the first line that matches the pattern.
exec will reject the promise if the timeout
is reached. The caught error will be of type
This will close the connection cleanly and leave the chat2 instance ready to be reconnected, if desired.
This program is called chat2 as it was inspired by Randal L. Schwartz' Perl 4 program chat2.pl from 1991.
The teletype module is similar but will lose data if it comes in too fast.
Please let me know of any issues at the Github Issues page.