diff --git a/package.json b/package.json index e3468a1cb455164ef20baf7dbe1498ccbcd410f6..f9d690e1b862a1e341199e693a364bcdf19cea94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "usp-js", - "version": "0.4.7", + "version": "0.4.8", "description": "Library for easy usp(TR-369) communication using mqtt or ws.", "main": "node/index.js", "browser": "web/index.js", diff --git a/src/configurations/mqtt.ts b/src/configurations/mqtt.ts index 7015f2d490deb6743a3cfd4e9b5a9881e128db01..9fe463c82f1815070b368d708354f39e106b8852 100644 --- a/src/configurations/mqtt.ts +++ b/src/configurations/mqtt.ts @@ -16,8 +16,15 @@ import { jsonRoots } from "./util"; export const isURL = (opts: ConnectionOptions): opts is URLConnectionOptions => "url" in opts; -export const connectClient: ConnectClientFn = async (opts) => - isURL(opts) +const correctOptions = (opts: ConnectionOptions): ConnectionOptions => ({ + ...opts, + clientId: opts.clientId || opts.fromId, + clean: opts.cleanSession ?? false, +}); + +export const connectClient: ConnectClientFn = async (initialOpts) => { + const opts = correctOptions(initialOpts); + return isURL(opts) ? (mqttAsync.connectAsync( opts.url, opts as any @@ -31,6 +38,7 @@ export const connectClient: ConnectClientFn = async (opts) => : {}), } as any) as unknown as ConnectionClient) : (mqttAsync.connectAsync(opts) as unknown as ConnectionClient); +}; export const loadProtobuf = async (version: USPVersion): Promise<Proto> => { const [rootRecordJson, rootMsgJson] = jsonRoots[version]; diff --git a/src/types.ts b/src/types.ts index a1efb7a4e37367d6f7ae895666988aabb27b1f40..73f68be2517622db188a86c98c50df3be55339d4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -765,6 +765,10 @@ export interface MainConnectionOptions { */ connectTimeout?: number; clientId?: string; + /** + * Defaults to false + */ + cleanSession?: boolean; /** * Allows passing additional options to the underlying connection library */