Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
You need to spend a bit more time reading documentation for your library. Take a look at this: https://github.com/websockets/ws#how-to-detect-and-close-broken-connections. In the example they have an interval loop outside of the 'connection' and 'message' loop. You'll need to rearrange your code and declare web socket server object as a variable so you can later refer to it. I have some methods that are run as the application starts (things like db connection, node server confuguration, seeding the db) and that's where you'd keep this sort of things - ideally in a separate method that configures WS server. Talking about documentation and web sockets, I have also found a lot of useful info here: https://rxjs.dev/api/webSocket/webSocket and here: https://rxjs.dev/api/webSocket/WebSocketSubjectConfig. That is assuming that you're using RxJs for handling your WS connection, which is my recommendation after spending a fair mount of time with WS last year.