Streams

To add a stream to your account:

To review the stream export:

  1. ╭──────────────────────────────────────────────────────────────────────────────────────╮
  2. Account Details
  3. ├───────────────────────────┬──────────────────────────────────────────────────────────┤
  4. Name A
  5. Account ID ADETPT36WBIBUKM3IBCVM4A5YUSDXFEJPW4M6GGVBYCBW7RRNFTV5NGE
  6. Issuer ID OAFEEYZSYYVI4FXLRXJTMM32PQEI3RGOWZJT7Y3YFM4HB7ACPE4RTJPG
  7. Issued 2019-12-05 13:35:42 UTC
  8. Expires
  9. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  10. Max Connections Unlimited
  11. Max Leaf Node Connections Unlimited
  12. Max Data Unlimited
  13. Max Exports Unlimited
  14. Max Imports Unlimited
  15. Max Msg Payload Unlimited
  16. Max Subscriptions Unlimited
  17. Exports Allows Wildcards True
  18. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  19. Imports None
  20. ╰───────────────────────────┴──────────────────────────────────────────────────────────╯
  21. ╭───────────────────────────────────────────────────────────╮
  22. Exports
  23. ├──────┬────────┬─────────┬────────┬─────────────┬──────────┤
  24. Name Type Subject Public Revocations Tracking
  25. ├──────┼────────┼─────────┼────────┼─────────────┼──────────┤
  26. abc Stream a.b.c.> Yes 0 N/A
  27. ╰──────┴────────┴─────────┴────────┴─────────────┴──────────╯

Messages this account publishes on a.b.c.> will be forwarded to all accounts that import this stream.

Importing a Stream

Importing a stream enables you to receive messages that are published by a different Account. To import a Stream, you have to create an Import. To create an Import you need to know:

  • The exporting account’s public key
  • The subject where the stream is published
  • You can map the stream’s subject to a different subject
  • Self-imports are not valid; you can only import streams from other accounts.

To learn how to inspect a JWT from an account server, check this article.

  1. > nsc add account B
  2. [ OK ] generated and stored account key "AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H"
  3. [ OK ] added account "B"
  4. > nsc add import --src-account ADETPT36WBIBUKM3IBCVM4A5YUSDXFEJPW4M6GGVBYCBW7RRNFTV5NGE --remote-subject "a.b.c.>"
  5. [ OK ] added stream import "a.b.c.>"

And verifying it:

  1. > nsc describe account
  2. ╭──────────────────────────────────────────────────────────────────────────────────────╮
  3. Account Details
  4. ├───────────────────────────┬──────────────────────────────────────────────────────────┤
  5. Name B
  6. Account ID AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H
  7. Issuer ID OAFEEYZSYYVI4FXLRXJTMM32PQEI3RGOWZJT7Y3YFM4HB7ACPE4RTJPG
  8. Issued 2019-12-05 13:39:55 UTC
  9. Expires
  10. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  11. Max Connections Unlimited
  12. Max Leaf Node Connections Unlimited
  13. Max Exports Unlimited
  14. Max Imports Unlimited
  15. Max Msg Payload Unlimited
  16. Max Subscriptions Unlimited
  17. Exports Allows Wildcards True
  18. Exports None
  19. ╰───────────────────────────┴──────────────────────────────────────────────────────────╯
  20. ╭─────────────────────────────────────────────────────────────────────────────╮
  21. Imports
  22. ├─────────┬────────┬─────────┬──────────────┬─────────┬──────────────┬────────┤
  23. Name Type Remote Local/Prefix Expires From Account Public
  24. ├─────────┼────────┼─────────┼──────────────┼─────────┼──────────────┼────────┤
  25. a.b.c.> Stream a.b.c.> A Yes
  26. ╰─────────┴────────┴─────────┴──────────────┴─────────┴──────────────┴────────╯

Let’s also add a user to make requests from the service:

  1. > nsc add user b
  2. [ OK ] generated and stored user key "UDKNTNEL5YD66U2FZZ2B3WX2PLJFKEFHAPJ3NWJBFF44PT76Y2RAVFVE"
  3. [ OK ] generated user creds file "~/.nkeys/creds/O/B/b.creds"
  4. [ OK ] added user "b" to account "B"

If you want to create a stream that is only accessible to accounts you designate you can create a private stream. The export will be visible in your account, but subscribing accounts will require an authorization token that must be created by you and generated specifically for the subscribing account.

The authorization token is simply a JWT signed by your account where you authorize the client account to import your export.

Creating a Private Stream Export

  1. > nsc add export --subject "private.abc.*" --private --account A
  2. [ OK ] added private stream export "private.abc.*"

Similarly when we defined an export, but this time we added the --private flag. The other thing to note is that the subject for the request has a wildcard. This enables the account to map specific subjects to specifically authorized accounts.

  1. > nsc describe account A
  2. ╭──────────────────────────────────────────────────────────────────────────────────────╮
  3. Account Details
  4. ├───────────────────────────┬──────────────────────────────────────────────────────────┤
  5. Name A
  6. Account ID ADETPT36WBIBUKM3IBCVM4A5YUSDXFEJPW4M6GGVBYCBW7RRNFTV5NGE
  7. Issuer ID OAFEEYZSYYVI4FXLRXJTMM32PQEI3RGOWZJT7Y3YFM4HB7ACPE4RTJPG
  8. Issued 2019-12-05 14:24:02 UTC
  9. Expires
  10. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  11. Max Connections Unlimited
  12. Max Leaf Node Connections Unlimited
  13. Max Data Unlimited
  14. Max Exports Unlimited
  15. Max Imports Unlimited
  16. Max Msg Payload Unlimited
  17. Max Subscriptions Unlimited
  18. Exports Allows Wildcards True
  19. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  20. Imports None
  21. ╰───────────────────────────┴──────────────────────────────────────────────────────────╯
  22. ╭──────────────────────────────────────────────────────────────────────────╮
  23. Exports
  24. ├───────────────┬────────┬───────────────┬────────┬─────────────┬──────────┤
  25. Name Type Subject Public Revocations Tracking
  26. ├───────────────┼────────┼───────────────┼────────┼─────────────┼──────────┤
  27. abc Stream a.b.c.> Yes 0 N/A
  28. private.abc.* Stream private.abc.* No 0 N/A
  29. ╰───────────────┴────────┴───────────────┴────────┴─────────────┴──────────╯

To generate a token, you’ll need to know the public key of the account importing the service. We can easily find the public key for account B by running:

  1. > nsc list keys --account B
  2. ╭──────────────────────────────────────────────────────────────────────────────────────────╮
  3. Keys
  4. ├────────┬──────────────────────────────────────────────────────────┬─────────────┬────────┤
  5. Entity Key Signing Key Stored
  6. ├────────┼──────────────────────────────────────────────────────────┼─────────────┼────────┤
  7. O OAFEEYZSYYVI4FXLRXJTMM32PQEI3RGOWZJT7Y3YFM4HB7ACPE4RTJPG *
  8. B AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H *
  9. ╰────────┴──────────────────────────────────────────────────────────┴─────────────┴────────╯
  1. > nsc generate activation --account A --target-account AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H --subject private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H -o /tmp/activation.jwt
  2. [ OK ] wrote account description to "/tmp/activation.jwt"

The command took the account that has the export (‘A’), the public key of account B, the subject where the stream will publish to account B.

For completeness, the contents of the JWT file look like this:

When decoded it looks like this:

  1. > nsc describe jwt -f /tmp/activation.jwt
  2. ╭────────────────────────────────────────────────────────────────────────────────────────╮
  3. Activation
  4. ├─────────────────┬──────────────────────────────────────────────────────────────────────┤
  5. Name private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H
  6. Account ID AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H
  7. Issuer ID ADETPT36WBIBUKM3IBCVM4A5YUSDXFEJPW4M6GGVBYCBW7RRNFTV5NGE
  8. Issued 2019-12-05 14:26:13 UTC
  9. Expires
  10. ├─────────────────┼──────────────────────────────────────────────────────────────────────┤
  11. Hash ID GWIS5YCSET4EXEOBXVMQKXAR4CLY4IIXFV4MEMRUXPSQ7L4YTZ4Q====
  12. ├─────────────────┼──────────────────────────────────────────────────────────────────────┤
  13. Import Type Stream
  14. Import Subject private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H
  15. ├─────────────────┼──────────────────────────────────────────────────────────────────────┤
  16. Max Messages Unlimited
  17. Max Msg Payload Unlimited
  18. Network Src Any
  19. Time Any
  20. ╰─────────────────┴──────────────────────────────────────────────────────────────────────╯

The token can be shared directly with the client account.

Importing a Private Stream

  1. > nsc add import --account B --token /tmp/activation.jwt
  2. [ OK ] added stream import "private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H"
  1. > nsc describe account B
  2. ╭──────────────────────────────────────────────────────────────────────────────────────╮
  3. Account Details
  4. ├───────────────────────────┬──────────────────────────────────────────────────────────┤
  5. Name B
  6. Account ID AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H
  7. Issuer ID OAFEEYZSYYVI4FXLRXJTMM32PQEI3RGOWZJT7Y3YFM4HB7ACPE4RTJPG
  8. Issued 2019-12-05 14:29:16 UTC
  9. Expires
  10. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  11. Max Connections Unlimited
  12. Max Leaf Node Connections Unlimited
  13. Max Data Unlimited
  14. Max Exports Unlimited
  15. Max Imports Unlimited
  16. Max Msg Payload Unlimited
  17. Max Subscriptions Unlimited
  18. Exports Allows Wildcards True
  19. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  20. Exports None
  21. ╰───────────────────────────┴──────────────────────────────────────────────────────────╯
  22. ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
  23. Imports
  24. ├──────────────────────────────────────────────────────────────────────┬────────┬──────────────────────────────────────────────────────────────────────┬──────────────┬─────────┬──────────────┬────────┤
  25. Name Type Remote Local/Prefix Expires From Account Public
  26. ├──────────────────────────────────────────────────────────────────────┼────────┼──────────────────────────────────────────────────────────────────────┼──────────────┼─────────┼──────────────┼────────┤
  27. a.b.c.> Stream a.b.c.> A Yes
  28. private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H Stream private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H A No
  29. ╰──────────────────────────────────────────────────────────────────────┴────────┴──────────────────────────────────────────────────────────────────────┴──────────────┴─────────┴──────────────┴────────╯

Testing the Private Stream

Testing a private stream is no different than a public one:

  1. > nsc sub --account B --user b private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H
  2. Listening on [private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H]
  3. ...
  4. > nsc pub --account A --user U private.abc.AAM46E3YF5WOZSE5WNYWHN3YYISVZOSI6XHTF2Q64ECPXSFQZROJMP2H hello