description: WebSocket Configuration Example

Configuration

To enable WebSocket support in the server, add a websocket configuration block in the server’s configuration file like the following:

  1. websocket {
  2. # Specify a host and port to listen for websocket connections
  3. #
  4. # listen: "host:port"
  5. # It can also be configured with individual parameters,
  6. # namely host and port.
  7. #
  8. # host: "hostname"
  9. port: 443
  10. # This will optionally specify what host:port for websocket
  11. # connections to be advertised in the cluster.
  12. #
  13. # advertise: "host:port"
  14. # TLS configuration is required by default
  15. #
  16. tls {
  17. cert_file: "/path/to/cert.pem"
  18. key_file: "/path/to/key.pem"
  19. }
  20. # For test environments, you can disable the need for TLS
  21. # by explicitly setting this option to `true`
  22. #
  23. # no_tls: true
  24. # [Cross-origin resource sharing option](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). When set to `true`, the HTTP origin header must match the request’s hostname.
  25. # The default is `false`.
  26. #
  27. # same_origin: true
  28. # [Cross-origin resource sharing option](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). List of accepted origins. When empty, and `same_origin` is `false`, clients from any origin are allowed to connect.
  29. # This list specifies the only accepted values for the client's request Origin header. The scheme,
  30. # host and port must match. By convention, the absence of TCP port in the URL will be port 80
  31. # for an "http://" scheme, and 443 for "https://".
  32. #
  33. # allowed_origins [
  34. # "http://www.example.com"
  35. # "https://www.other-example.com"
  36. # ]
  37. # This enables support for compressed websocket frames
  38. # in the server. For compression to be used, both server
  39. # and client have to support it.
  40. #
  41. # compression: true
  42. # This is the total time allowed for the server to
  43. # read the client request and write the response back
  44. # to the client. This includes the time needed for the
  45. # TLS handshake.
  46. #
  47. # handshake_timeout: "2s"
  48. # Name for an HTTP cookie, that if present will be used as a client JWT.
  49. # If the client specifies a JWT in the CONNECT protocol, this option is ignored.
  50. # The cookie should be set by the HTTP server as described [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies).
  51. # This setting is useful when generating NATS `Bearer` client JWTs as the
  52. # result of some authentication mechanism. The HTTP server after correct
  53. # authentication can issue a JWT for the user, that is set securely preventing
  54. # access by unintended scripts. Note these JWTs must be [NATS JWTs](https://docs.nats.io/nats-server/configuration/securing_nats/jwt).
  55. #
  56. # jwt_cookie: "my_jwt_cookie_name"
  57. # If no user name is provided when a websocket client connects, will default
  58. # this user name in the authentication phase. If specified, this will
  59. # override, for websocket clients, any `no_auth_user` value defined in the
  60. # main configuration file.
  61. # Note that this is not compatible with running the server in operator mode.
  62. #
  63. # no_auth_user: "my_username_for_apps_not_providing_credentials"
  64. # See below to know what is the normal way of limiting websocket clients
  65. # to specific users.
  66. # If there are no users specified in the configuration, this simple authorization
  67. # block allows you to override the values that would be configured in the
  68. # equivalent block in the main section.
  69. #
  70. # authorization {
  71. # # If this is specified, the client has to provide the same username
  72. # # and password to be able to connect.
  73. # # username: "my_user_name"
  74. # # password: "my_password"
  75. #
  76. # # If this is specified, the password field in the CONNECT has to
  77. # # match this token.
  78. # # token: "my_token"
  79. #
  80. # # This overrides the main's authorization timeout. For consistency
  81. # # with the main's authorization configuration block, this is expressed
  82. # # as a number of seconds.
  83. # # timeout: 2.0
  84. #}
  85. }

Authorization of WebSocket Users

A new field when configuring users allows you to restrict which type of connections are allowed for a specific user.

Consider this configuration:

  1. authorization {
  2. users [
  3. {user: foo password: foopwd, permission: {...}}
  4. {user: bar password: barpwd, permission: {...}}
  5. ]
  6. }

If a WebSocket client were to connect and use the username foo and password foopwd, it would be accepted. Now suppose that you would want the WebSocket client to only be accepted if it connected using the username bar and password barpwd, then you would use the option allowed_connection_types to restrict which type of connections can bind to this user.

  1. authorization {
  2. users [
  3. {user: foo password: foopwd, permission: {...}}
  4. {user: bar password: barpwd, permission: {...}, allowed_connection_types: ["WEBSOCKET"]}
  5. ]
  6. }

The option allowed_connection_types (also can be named connection_types or clients) as you can see is a list, and you can allow several types of clients. Suppose you want the user bar to accept both standard NATS clients and WebSocket clients, you would configure the user like this:

  1. authorization {
  2. users [
  3. {user: foo password: foopwd, permission: {...}}
  4. {user: bar password: barpwd, permission: {...}, allowed_connection_types: ["STANDARD", "WEBSOCKET"]}
  5. ]
  6. }

The absence of allowed_connection_types means that all types of connections are allowed (the default behavior).

The possible values are currently:

  • STANDARD
  • WEBSOCKET
  • LEAFNODE
  • MQTT

Leaf nodes connections

You can configure remote Leaf node connections so that they connect to the Websocket port instead of the Leaf node port. See Leafnode section.