Skip to content

Retain retained messages and send them to new subscriptions #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/mqtt-pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PubSubEngine } from 'graphql-subscriptions/dist/pubsub-engine';
import { connect, Client, ISubscriptionGrant, IClientPublishOptions, IClientSubscribeOptions } from 'mqtt';
import { connect, Client, ISubscriptionGrant, IClientPublishOptions, IClientSubscribeOptions, IPublishPacket } from 'mqtt';
import { PubSubAsyncIterator } from './pubsub-async-iterator';

export interface PubSubMQTTOptions {
Expand All @@ -22,6 +22,7 @@ export class MQTTPubSub implements PubSubEngine {
private mqttConnection: Client;
private subscriptionMap: { [subId: number]: [string, Function] };
private subsRefsMap: { [trigger: string]: Array<number> };
private retainedMessagesMap: { [trigger: string]: any };
private currentSubscriptionId: number;
private parseMessageWithEncoding: string;

Expand Down Expand Up @@ -70,6 +71,7 @@ export class MQTTPubSub implements PubSubEngine {

this.subscriptionMap = {};
this.subsRefsMap = {};
this.retainedMessagesMap = {};
this.currentSubscriptionId = 0;
this.onMQTTSubscribe = options.onMQTTSubscribe || (() => null);
this.publishOptionsResolver = options.publishOptions || (() => Promise.resolve({} as IClientPublishOptions));
Expand All @@ -95,6 +97,13 @@ export class MQTTPubSub implements PubSubEngine {
if (refs && refs.length > 0) {
const newRefs = [...refs, id];
this.subsRefsMap[triggerName] = newRefs;
// If we have a retained message then send it to this new subscription
const retainedMessage = this.retainedMessagesMap[triggerName];
if (retainedMessage) {
setTimeout(() => {
onMessage(retainedMessage);
}, 0);
}
return Promise.resolve(id);

} else {
Expand Down Expand Up @@ -136,6 +145,9 @@ export class MQTTPubSub implements PubSubEngine {
if (refs.length === 1) {
this.mqttConnection.unsubscribe(triggerName);
newRefs = [];
// Delete the retained messages for this subscription
// we will get sent any retained messages when we resubscribe
delete this.retainedMessagesMap[triggerName];

} else {
const index = refs.indexOf(subId);
Expand All @@ -152,7 +164,7 @@ export class MQTTPubSub implements PubSubEngine {
return new PubSubAsyncIterator<T>(this, triggers);
}

private onMessage(topic: string, message: Buffer) {
private onMessage(topic: string, message: Buffer, packet: IPublishPacket) {
const subscribers = [].concat(
...Object.keys(this.subsRefsMap)
.filter((key) => MQTTPubSub.matches(key, topic))
Expand All @@ -175,6 +187,10 @@ export class MQTTPubSub implements PubSubEngine {
const listener = this.subscriptionMap[subId][1];
listener(parsedMessage);
}
if (packet.retain) {
// Retain the message if it is meant to be retained
this.retainedMessagesMap[topic] = parsedMessage
}
}
}

Expand Down