DZone
IoT Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > IoT Zone > NATS, What a Beautiful Protocol

NATS, What a Beautiful Protocol

Brian Flannery follows along with Daniel Wertheim's blog post about the NATS protocol.

Brian Flannery user avatar by
Brian Flannery
·
May. 22, 16 · IoT Zone · Opinion
Like (4)
Save
Tweet
9.16K Views

Join the DZone community and get the full member experience.

Join For Free

One of the nice things about NATS.io (an open source messaging system for microservices, IoT, and Cloud Native messaging) is the simplicity of the protocol. As a developer, you can very quickly get NATS running and understand it; this is in contrast to traditional messaging systems, which can introduce complexity and operational complexity. This focus on simplicity makes it very well suited for modern distributed systems.

Daniel Wertheim is a community developer who has written several excellent posts on his blog about NATS. 

Here is the 1st in his series of posts.

--

Just had a quick glance at NATS and its protocol and I said: "OMG, it's like so simple and therefore so tremendously beautiful. Why? I told you. It's simple. And I can read it. As in read it and understand it without having to read a boring specification about a binary fixed protocol format."

New relevant post: "Simple incoming OP parser for Nats in C#"

Without any deep thoughts (so you can most certainly laugh at my hacky bits), using C#, I just put down something simple that publishes messages to a subscriber in Telnet:

Func<string, byte[]> encode = Encoding.UTF8.GetBytes;  
Func<byte[], string> decode = Encoding.UTF8.GetString;

Func<Socket, byte[]> read = socket =>  
{
    var buff = new byte[256];
    var r = socket.Receive(buff);

    return buff.Take(r).ToArray();
};

Action<Socket> dump = s =>  
{
    var buff = read(s);
    Console.WriteLine(buff.Any()
        ? decode(buff)
        : "--nothing--");
};

Action<Socket, string> pub = (s, m) =>  
    s.Send(encode($"pub foo {m.Length}\r\n{m}\r\n"));

using (var tcp = new TcpClient())  
{
    tcp.Connect("demo.nats.io", 4222);

    dump(tcp.Client);

    while (true)
    {
        var msg = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(msg))
            break;

        pub(tcp.Client, msg);

        dump(tcp.Client);
    }

    tcp.Close();
}


So now, if you start a telnet session and go:

telnet demo.nats.io 4222  
sub foo subId123  


Then take the C# code and run it in e.g. a console application, then you would be able to do this:


Simple, right? Which seems to be the characteristics of NATS. It's trying to be simple in the way that it doesn't try to support every use case found in an enterprise scenario. But, hey, I just started looking into it.

Cheers,

//Daniel

Nat (unit) Protocol (object-oriented programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • Message Queuing and the Database: Solving the Dual Write Problem
  • How to Handle Early Startup Technical Debt (Or Just Avoid it Entirely)
  • 6 Best Books to Learn Multithreading and Concurrency in Java

Comments

IoT Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo