This challenge corresponds to the 25th part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-nats.
This is a Node.js implementation for the NATS server protocol. The server supports the following client commands:
-
CONNECT (with additional args and
verbose
support) -
PUB (Basic support excluding
reply-to
arg) -
SUB (Basic support excluding
queue-group
arg) -
UNSUB (Basic support excluding
max_msgs
arg)
-
Created a NATS message Parser using TDD (
parser.ts
). A new method: 'Zero allocation byte parsing' was explored here. The original NATS parser is written ingo
can be found here. -
Created server.ts to handle CONNECT, PING and PONG commands. Also created client.ts to store information regarding a NATS client.
-
Created different classes for topics and subscriptions.
-
Added functionality for PUB and SUB commands.
-
Added functionality for UNSUB command.
-
Added E2E tests for all the commands using the NATS.js client.
You can use the ts-node
tool to run the NATS server as follows:
npx ts-node index.ts [--debug]
The NATS server will start on port 4222 by default.
To test the NATS server, open a new terminal and start a telnet session using the following command:
telnet localhost 4222
As soon as the client is connect, you should see a INFO message sent by the server.
You should be able to do something like this (the client is both the publisher and the subscriber):
CONNECT {}
+OK
SUB CC 10
+OK
PUB CC 4
MJ
+OK
MSG CC 10 4
MJ
To run the tests for the NATS server, go to the root directory of this repository and run the following command:
npm test src/25/
- Handle the case when a client sends a PUB command with valid topic name is found.
- Handle the case when a client sends a UNSUB command with no valid subscription or topic is found.