Skip to content

Latest commit

 

History

History

25

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Challenge 25 - Write Your Own NATS Message Broker

This challenge corresponds to the 25th part of the Coding Challenges series by John Crickett https://codingchallenges.fyi/challenges/challenge-nats.

Description

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)

  • PING

  • PUB (Basic support excluding reply-to arg)

  • SUB (Basic support excluding queue-group arg)

  • UNSUB (Basic support excluding max_msgs arg)

Approach

  1. 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 in go can be found here.

  2. Created server.ts to handle CONNECT, PING and PONG commands. Also created client.ts to store information regarding a NATS client.

  3. Created different classes for topics and subscriptions.

  4. Added functionality for PUB and SUB commands.

  5. Added functionality for UNSUB command.

  6. Added E2E tests for all the commands using the NATS.js client.

Usage

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

Run tests

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/

TODOs

  • 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.