This is an attempt to emulate peer to peer communicaton on a distributed system using NodeJS and TypeScript.
Users creates their 'account' on their own URI while seed servers are created on predetermined URIs(Others can be added if needed).
With a minimum of 2 users, they can be registered on the same seed servers or each on their own seed server randomly.
Each seed server is aware of the other and the users registered under it for purposes of communication.
When a user decides to send a message, the user is searched in the seed servers and once found, the URI is then returned to the sending server which then sends the message directly to the intended audience.
The followingare the base requirements and the versions at the time of creating this repository.
- Node (v17.0.1)
- TypeScript (Version 5.0.4)
Start the servers by running the following commands in the terminal:
USER_NAME=seedX PORT=5001 npm run dev
This creates a seed server called seedX at port 5001
. You can run this on different terminals to create as many seed servers as you wish but be sure to change the USER_NAME and PORT variables.
To create a user, You can run one of the following commands in the terminal:
-
USER_NAME=UserX npm run dev
This will create a UserX on the default port that you serve the project on. -
PORT=5012 USER_NAME=Bobby npm run dev
This will create a different user at port5012
with the name Bobby
You can use the second opton to create as many users as you wish to have in your test.
To be able to send messages to users registered on the nodes, you can use Postman, Insomnia or another API testing client of your choice to hit the /send
endpoint.
Example: Assuming our user Bobby is started on port 5012, we can then navigate to localhost:5012/send
to send a message to UserX who is registered on localhost:3000
.
The structure of the message will be as follows:
{
"message":"Hello UserX, It is nice to meet you",
"to":"UserX"
}
Once you submit your request, the seed servers will search for a user with the matching username and if the name is not found on the first seed server, the requst is send to the next seed server available until the user is found. If no user is found mathcing the name in all the servers, you will get a User Not Found
message.