NATs lets you send messages to a subject. Like a HTTP request it takes a body and headers.
NATs also lets you subscribe to subjects. It gets the body, headers from a publish request and can process the message and send a reply a reply subject specified within the message.
Every message comes with a reply subject; it creates a short lived subscription to a randomly generated inbox. This allows a subscription to know where to send a response and creates the publish / subscribe pattern.
Create a subscriber -> nats sub "*" --queue="rpc" Publish a message -> nats pub rpc/hello/there 'hello there'
Catch all will see everything that comes in and tell you the endpoint it is coming from.
Sub - nats sub "rpc/>" --queue="rpc" -> this should let you see any messages that start with rpc/ and end with anything else Pub nats pub rpc/hello/there 'hello there' -> see it doesn't work
Sub - nats sub "rpc.>" --queue="rpc" Pub - nats pub rpc.hello.there 'hello there' -> receives message
NATs has a few ways to authenticate a connection to the server. I'm going to focus on one of the easier ones of username / password but other options include: token, NKey, File Credentials and TLS. When setting your username and password you connect to the NATs cluster; it will check its user configuration to understand what the connection has permission to.
We have 3 main services apart of Twist
- RGS - This is our key API all traffic goes through here.
- Game Engines - these are all the same except with different names. They give a bet result from the game to give to the RGS.
- RNG server - This generate our random numbers for the game.
We have these rules that we must achieve
- RGS can send requests to Game. -- subject
- RGS can receive replies from the Game Engine.
- RGS can reply to a Game.
- Game can send replies to the RGS
- Game can send request to the RGS
- Game can send request to the RNG
- Game can receive replies from RNG
- _INBOX.> allows you to connect to every inbox and listen to subscribe to other inboxes RESPONSE messages
- _INBOX.{servicename}.> - Making your subscribe subject for inboxes helps protect against this.
- _INBOX.> is needed for publish permissions unless you map all the other services that will send messages to this users subscription. A lot of effort.
A few commands:
nats sub "rpc.>" --user=god --password=god nats sub "_INBOX.>" --user=god --password=god
nats sub "" --user=rng --password=ExQDVTjkJvpNpNvC6k6Ly0qVzF3OvoODXG39Bdzv nats sub "rpc." --user=rng --password=ExQDVTjkJvpNpNvC6k6Ly0qVzF3OvoODXG39Bdzv nats sub "rpc.1_1_97.play" --user=rng --password=ExQDVTjkJvpNpNvC6k6Ly0qVzF3OvoODXG39Bdzv
nats sub "rpc.rng.integers" --user=rng --password=ExQDVTjkJvpNpNvC6k6Ly0qVzF3OvoODXG39Bdzv
nats pub rpc.hello.there 'hello there' --user=god --password=god nats pub rpc.rng.integers 'hello there' --user=god --password=god
nats pub rpc.rng.integers 'hello there' --user=rgs --password=t7sueqyO1PU14utllwe3hJSPVX0sqP7e9xljNyga