how to create ws nats server in go and connect

263 views
Skip to first unread message

Burak Akça

unread,
Sep 6, 2022, 6:14:34 AM9/6/22
to nats
I want to create websocket nats server and i'm using this example

- I change this example with below code;

tlsConfig := &tls.Config{
Certificates: certs,
}
opts := &server.Options{}
opts.Websocket.Host = "127.0.0.1"
opts.Websocket.TLSConfig = tlsConfig
opts.Websocket.Port = 8080
ns, err := server.NewServer(opts)

sudo lsof -i:8080
main    1081038 burak   10u  IPv4 4243850      0t0  TCP localhost:http-alt (LISTEN)

But when i want to connect to this server  from browser (nats.ws), i get this;

ws_transport.js?2234:73 WebSocket connection to 'wss://0.0.0.0:8080/' failed:
eval @ ws_transport.js?2234:73
eval @ ws_transport.js?2234:22
__awaiter @ ws_transport.js?2234:18
connect @ ws_transport.js?2234:56
eval @ protocol.js?2766:183
eval @ protocol.js?2766:8
__awaiter @ protocol.js?2766:4
dial @ protocol.js?2766:178
eval @ protocol.js?2766:244
fulfilled @ protocol.js?2766:5
ws_transport.js?2234:124 Uncaught (in promise) NatsError
    at socket.onerror (ws_transport.js?2234:124:1)



Alberto Ricart

unread,
Sep 6, 2022, 10:22:46 AM9/6/22
to nats
What version of the library are you running? Also make sure that your certificates are trusted by the browser. If you catch the error, do you get a better exception?

iv...@synadia.com

unread,
Sep 6, 2022, 11:03:54 AM9/6/22
to nats
I would instead generate the TLS configuration with something like that:
```
tc := &TLSConfigOpts{
CertFile: "../test/configs/certs/server-cert.pem",
KeyFile: "../test/configs/certs/server-key.pem",
CaFile: "../test/configs/certs/ca.pem",
}
tlsConfig, err := GenTLSConfig(tc)
```
And be advised that NewServer() is not enough, you also need to call Start(), but I assume that you do that since you pointed at an example code that does it.
Message has been deleted

Burak Akça

unread,
Sep 7, 2022, 3:46:00 AM9/7/22
to nats
- "nats.ws": "1.9.0"

Certificates created with mkcert 

Alberto Ricart

unread,
Sep 7, 2022, 12:46:11 PM9/7/22
to nats
Reply all
Reply to author
Forward
0 new messages