-
Notifications
You must be signed in to change notification settings - Fork 22
/
CompressedPublisher.js
185 lines (169 loc) · 7.01 KB
/
CompressedPublisher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* Solace Systems Node.js API
* Publish/Subscribe tutorial - Compressed Publisher
*
* Minor variation on the Topic Publisher tutorial: adds compression.
*
* Compression is compatible with tcp and tcps.
* Available zlib compression levels are 1-9,
* setting the compressionLevel to 0 disables compression.
*
* Compressed tcp uses a separate port, by default 55003.
* The API uses the right default, so it is simplest not to override
* unless the router uses non-standard ports.
*
* Compressed tcps uses the regular tcps port, by default 55443.
* Compression is perfomed before encryption,
* so this combination should not be used in environments
* open to Chosen Plaintext Attacks.
*/
/*jslint es6 node:true devel:true*/
var CompressedPublisher = function (solaceModule, topicName) {
'use strict';
var solace = solaceModule;
var publisher = {};
publisher.session = null;
publisher.topicName = topicName;
// Logger
publisher.log = function (line) {
var now = new Date();
var time = [('0' + now.getHours()).slice(-2), ('0' + now.getMinutes()).slice(-2),
('0' + now.getSeconds()).slice(-2)];
var timestamp = '[' + time.join(':') + '] ';
console.log(timestamp + line);
};
publisher.log('\n*** Publisher to topic "' + publisher.topicName + '" is ready to connect ***');
// main function
publisher.run = function (argv) {
publisher.connect(argv);
};
// Establishes connection to Solace PubSub+ Event Broker
publisher.connect = function (argv) {
if (publisher.session !== null) {
publisher.log('Already connected and ready to publish.');
return;
}
// extract params
if (argv.length < (2 + 3)) { // expecting 3 real arguments
publisher.log('Cannot connect: expecting all arguments' +
' <protocol://host[:port]> <client-username>@<message-vpn> <client-password>.\n' +
'Available protocols are tcp:// and tcps://');
process.exit();
}
var hosturl = argv.slice(2)[0];
publisher.log('Connecting to Solace PubSub+ Event Broker using url: ' + hosturl);
var usernamevpn = argv.slice(3)[0];
var username = usernamevpn.split('@')[0];
publisher.log('Client username: ' + username);
var vpn = usernamevpn.split('@')[1];
publisher.log('Solace PubSub+ Event Broker VPN name: ' + vpn);
var pass = argv.slice(4)[0];
// create session
try {
publisher.session = solace.SolclientFactory.createSession({
// solace.SessionProperties
url: hosturl,
vpnName: vpn,
userName: username,
password: pass,
sslValidateCertificate: false,
// Enables maximum compression.
compressionLevel: 9
});
} catch (error) {
publisher.log(error.toString());
}
// define session event listeners
publisher.session.on(solace.SessionEventCode.UP_NOTICE, function (sessionEvent) {
publisher.log('=== Successfully connected and ready to publish messages. ===');
publisher.publish();
publisher.exit();
});
publisher.session.on(solace.SessionEventCode.CONNECT_FAILED_ERROR, function (sessionEvent) {
publisher.log('Connection failed to the message router: ' + sessionEvent.infoStr +
' - check correct parameter values and connectivity!');
});
publisher.session.on(solace.SessionEventCode.DISCONNECTED, function (sessionEvent) {
publisher.log('Disconnected.');
if (publisher.session !== null) {
publisher.session.dispose();
publisher.session = null;
}
});
// connect the session
try {
publisher.session.connect();
} catch (error) {
publisher.log(error.toString());
}
};
// Publishes one message
publisher.publish = function () {
if (publisher.session !== null) {
var messageText = 'Sample Message';
var message = solace.SolclientFactory.createMessage();
message.setDestination(solace.SolclientFactory.createTopicDestination(publisher.topicName));
message.setBinaryAttachment(messageText);
message.setDeliveryMode(solace.MessageDeliveryModeType.DIRECT);
publisher.log('Publishing message "' + messageText + '" to topic "' + publisher.topicName + '"...');
try {
publisher.session.send(message);
publisher.log('Message published.');
} catch (error) {
publisher.log(error.toString());
}
} else {
publisher.log('Cannot publish because not connected to Solace PubSub+ Event Broker.');
}
};
publisher.exit = function () {
publisher.disconnect();
setTimeout(function () {
process.exit();
}, 1000); // wait for 1 second to finish
};
// Gracefully disconnects from Solace PubSub+ Event Broker
publisher.disconnect = function () {
publisher.log('Disconnecting from Solace PubSub+ Event Broker...');
if (publisher.session !== null) {
try {
publisher.session.disconnect();
} catch (error) {
publisher.log(error.toString());
}
} else {
publisher.log('Not connected to Solace PubSub+ Event Broker.');
}
};
return publisher;
};
var solace = require('solclientjs').debug; // logging supported
// Initialize factory with the most recent API defaults
var factoryProps = new solace.SolclientFactoryProperties();
factoryProps.profile = solace.SolclientFactoryProfiles.version10;
solace.SolclientFactory.init(factoryProps);
// enable logging to JavaScript console at WARN level
// NOTICE: works only with ('solclientjs').debug
solace.SolclientFactory.setLogLevel(solace.LogLevel.WARN);
// create the publisher, specifying the name of the subscription topic
var publisher = new CompressedPublisher(solace, 'tutorial/topic');
// publish message to Solace PubSub+ Event Broker
publisher.run(process.argv);