Skip to content
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

MF-429 -Enabled MQTT subtopic's #554

Merged
merged 4 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
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
27 changes: 23 additions & 4 deletions mqtt/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ nats.subscribe('channel.*', function (msg) {

aedes.authorizePublish = function (client, packet, publish) {
// Topics are in the form `channels/<channel_id>/messages`
var channel = /^channels\/(.+?)\/messages$/.exec(packet.topic);
// Subtopic's are in the form `channels/<channel_id>/messages/<subtopic>`
var channel = /^channels\/(.+?)\/messages\/?(.+?)?$/.exec(packet.topic);
if (!channel) {
logger.warn('unknown topic');
publish(4); // Bad username or password
Expand All @@ -109,6 +110,18 @@ aedes.authorizePublish = function (client, packet, publish) {
token: client.password,
chanID: channelId
},
// Parse unlimited subtopics
baseLength = 3, // First 3 elements which represents the base part of topic.
elements = packet.topic.split('/').slice(baseLength),
baseTopic = 'channel.' + channelId;
// Remove empty elements
for (var i = 0; i < elements.length; i++) {
if (elements[i] === '') {
elements.pop(i)
}
}
var channelTopic = elements.length ? baseTopic + '.' + elements.join('.') : baseTopic,

onAuthorize = function (err, res) {
var rawMsg;
if (!err) {
Expand All @@ -120,7 +133,7 @@ aedes.authorizePublish = function (client, packet, publish) {
Protocol: 'mqtt',
Payload: packet.payload
});
nats.publish('channel.' + channelId, rawMsg);
nats.publish(channelTopic, rawMsg);

publish(0);
} else {
Expand All @@ -135,8 +148,14 @@ aedes.authorizePublish = function (client, packet, publish) {

aedes.authorizeSubscribe = function (client, packet, subscribe) {
// Topics are in the form `channels/<channel_id>/messages`
var channel = /^channels\/(.+?)\/messages$/.exec(packet.topic),
channelId = channel[1],
// Subtopic's are in the form `channels/<channel_id>/messages/<subtopic>`
var channel = /^channels\/(.+?)\/messages\/?(.+?)?$/.exec(packet.topic);
drasko marked this conversation as resolved.
Show resolved Hide resolved
if (!channel) {
logger.warn('unknown topic');
subscribe(4, packet); // Bad username or password
return;
}
var channelId = channel[1],
accessReq = {
token: client.password,
chanID: channelId
Expand Down
2 changes: 1 addition & 1 deletion normalizer/nats/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

const (
queue = "normalizers"
input = "channel.*"
input = "channel.>"
nmarcetic marked this conversation as resolved.
Show resolved Hide resolved
outputUnknown = "out.unknown"
senML = "application/senml+json"
)
Expand Down