MQTT restart the service after few seconds

I use MQTT 4.3.8 on my TV (because nodejs version is 12).
Mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.

The connection and subscribe to a topic on mosquitto server is successfull. The server log shows that the connection is closed after few seconds.
In simulator everithing worked fine.
What can I do for solve this problem?

const mqtt = require("mqtt");
const pkgInfo = require("./package.json");
const Service = require("webos-service");
const service = new Service(pkgInfo.name);
var client = null;
var lastMessage = null;
var lastErrorMessage = "uninitialized";

// a method that always returns the same value
service.register("mqttconnect", function (message) {
	try {
		client = mqtt.connect("mqtt://my.local.server");

		client.on("connect", () => {
			client.subscribe("clipboard", (err) => {
				if (!err) {
					console.log("Subscribed to clipboard");
					lastMessage = "Subscribed to clipboard";
				} else {
					console.log("Error subscribing to clipboard: " + err);
					lastErrorMessage = JSON.stringify(err);
				}
			});
		});

		client.on("error", (error) => {
			lastErrorMessage = "Error on MQTT connection: " + error;
		});

		client.on("close", () => {
			lastErrorMessage = "MQTT connection closed";
		});

		client.on("message", (topic, message) => {
			try {
				lastMessage = message.toString();
				if (message.toString().startsWith("http")) {
					service.call(
						"luna://com.webos.applicationManager/launch",
						{
							id: "com.webos.app.browser",
							params: {
								target: message.toString(),
							},
						},
						function (response) {
							console.log(
								"Response from launch",
								JSON.stringify(response)
							);
							lastErrorMessage =
								"Launcg browser result: " +
								JSON.stringify(response);
						}
					);
				}

				lastMessage = "last message:" + message;
			} catch (e) {
				console.log("Error on message: " + e);
				lastErrorMessage = "Error on message:" + JSON.stringify(e);
			}
		});

		message.respond({
			returnValue: true,
			message: "MQTT Client connected",
		});
	} catch (e) {
		message.respond({
			returnValue: false,
			message: "MQTT Client connection failed: " + e,
		});
	}
});

service.register("mqttdisconnect", function (message) {
	try {
		client.end();
		message.respond({
			returnValue: true,
			message: "MQTT Client disconnected",
		});
	} catch (e) {
		message.respond({
			returnValue: false,
			message: "MQTT Client disconnection failed: " + e,
		});
	}
});

service.register("getlastmessage", function (message) {
	message.respond({
		returnValue: true,
		message: lastMessage,
	});
});

service.register("getlasterrormessage", function (message) {
	message.respond({
		returnValue: true,
		message: lastErrorMessage,
	});
});

I tried debuging the service on TV, but I get error message:

Cannot support "--open option" on platform node version 8 and later
To debug your service, set "localhost:56398" on Node Inspector Client(Chrome DevTools, Visual Studio Code, etc.).

If I sent message between successfull connection and service restart, the message arrived to mqtt topic and browser opened with parameter. I don’t know why restart the service after 3-4 second.

Ok, I started the Chrome DevTools. And I can debug the service.

What does this line mean?

no active activities, would exit, but timeout is disabled

When I start the inspector with

ares-inspect --device LG43 --service hu.exprog.mqttclient.service

the service doesn’t restart.

Sorry for the late reply. It seems that there is a service that cannot be terminated. Please refer to Q3 and Q4 in JavaScript Service FAQ. Thank you.