Skip to content

Commit 04346cd

Browse files
authored
Merge pull request #2 from joelmenezes/Access_Remote_Queues
Added logic to receive messages from remote queue. Refactored logic t…
2 parents 493ac5b + be3868d commit 04346cd

File tree

5 files changed

+77
-51
lines changed

5 files changed

+77
-51
lines changed

MSMQLib/MSMQInterface.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,12 @@ public async Task<object> CreateQueue(string path)
2727
}
2828

2929
/// <summary>
30-
/// Sends a message to a remote MSMQ queue.
30+
/// Creates a MSMQ queue.
3131
/// </summary>
32-
/// <param name="input">The queue's path and the message.</param>
33-
/// <returns>True if queue can be connected to.</returns>
34-
public async Task<object> SendToRemoteQueue(dynamic input)
32+
/// <param name="path">The remote queue's path.</param>
33+
/// <returns>True.</returns>
34+
public async Task<object> ConnectRemote(string path)
3535
{
36-
string path = (string)input.path;
37-
string message = (string)input.message;
38-
39-
byte[] bytes = Encoding.UTF8.GetBytes(message);
40-
MessageQueue queue = new MessageQueue(path);
41-
Message msg = new Message();
42-
msg.BodyStream = new MemoryStream(bytes);
43-
44-
await Task.Run(() => queue.Send(msg));
4536
return true;
4637
}
4738

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "updated-node-msmq",
3-
"version": "0.1.6",
3+
"version": "0.1.9",
44
"description": "A MSMQ implementation for node.js",
55
"main": "dist/index.js",
66
"scripts": {

proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ export var queueProxy = {
1717
receive: getMethod('ReceiveMessages'),
1818
list: getMethod('GetAllMessages'),
1919
clear: getMethod('PurgeQueue'),
20-
sendRemote: getMethod('SendToRemoteQueue')
20+
connectRemote: getMethod('ConnectRemote')
2121
};

queue.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@ export default class Queue extends EventEmitter {
2727
return new Queue(path);
2828
}
2929

30-
static sendToRemoteQueue(path, message, cb) {
31-
let formattedMessage = JSON.stringify(message);
32-
33-
return queueProxy.sendRemote({
34-
path: path,
35-
message: formattedMessage
36-
}, cb);
30+
static connectToRemoteQueue(path) {
31+
queueProxy.connectRemote(path, true);
32+
return new Queue(path);
3733
}
3834

3935
startReceiving() {

readme.md

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ NPM package is published as updated-node-msmq. https://www.npmjs.com/package/upd
1010

1111
* Support for Node.Js 6.x, 7.x, 8.x, 9.x, 10.x
1212
* Support to push objects to the queue instead of just strings.
13-
* Support to send messages to a queue on a **remote** machine.
13+
* Support to send/receive messages to/from a queue on a **remote** machine.
1414

1515
## Install
1616

1717
```
1818
$ npm install --save updated-node-msmq
1919
```
2020

21-
## Usage
21+
## Usage (Local Queue)
2222

2323
### Send a message
2424

@@ -33,6 +33,48 @@ var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
3333
queue.send('Hello from Node.JS!');
3434
```
3535

36+
### Receive messages
37+
38+
Start receiving messages from a queue.
39+
40+
```js
41+
const msmq = require('updated-node-msmq');
42+
43+
var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
44+
45+
// Set receive listener callback
46+
queue.on('receive', (msg) => {
47+
console.log(msg.body);
48+
});
49+
50+
// Start receiving messages from the queue
51+
queue.startReceiving();
52+
```
53+
54+
### Get all messages
55+
56+
Gets all messages without removing them from queue.
57+
58+
```js
59+
const msmq = require('updated-node-msmq');
60+
61+
var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
62+
var messages = queue.getAllMessages();
63+
```
64+
65+
### Purge a queue
66+
67+
Clears all messages from the queue.
68+
69+
```js
70+
const msmq = require('updated-node-msmq');
71+
72+
var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
73+
queue.purge();
74+
```
75+
76+
## Usage (Remote Queue)
77+
3678
### Send a message to a remote queue
3779

3880
Sends a message to a remote MSMQ queue.
@@ -41,35 +83,26 @@ Sends a message to a remote MSMQ queue.
4183
const msmq = require('updated-node-msmq');
4284

4385
// Send message to a remote queue using hostname
44-
msmq.sendToRemoteQueue('FormatName:DIRECT=OS:mobile-000000\\private$\\privatetest', 'Hello from Node.JS!');
86+
let queue1 = msmq.connectToRemoteQueue('FormatName:DIRECT=OS:mobile-000000\\private$\\privatetest');
4587

46-
msmq.sendToRemoteQueue('FormatName:DIRECT=TCP:192.168.1.5\\private$\\privatetest', 'Hello again from Node.JS!');
47-
```
88+
queue1.send('Hello again from Node.JS!');
4889

49-
#### Note:
50-
* Creating a queue on a remote machine is not currently supported by MSMQ.
51-
* To send messages to a remote queue, MSMQ should be enabled in the sender's machine too. Also, in the _Security_ tab of the queue on the remote machine should have the appropriate permissions set for _Everyone_ and _ANONYMOUS LOGON_.
52-
* The queue should already be created on the remote machine.
53-
* The format to send a message to a remote queue is as follows:
54-
`
55-
msmsq.sendToRemoteQueue(path, message);
56-
`
57-
* `path` has to be in the following format:
90+
// Send message to a remote queue using IP address
91+
let queue2 = msmq.connectToRemoteQueue('FormatName:DIRECT=TCP:192.168.5.21\\private$\\privatetest');
5892

59-
`FormatName:DIRECT=TCP:`_`<ip_address>`_`\\private$\\`_`<queue_name>`_`
93+
queue2.send('Hello again from Node.JS!');
6094

61-
or
6295

63-
`FormatName:DIRECT=OS:`_`<machine_name>`_`\\private$\\`_`<queue_name>`_`
96+
```
6497

65-
### Receive messages
98+
### Receive messages from a remote queue
6699

67-
Start receiving messages from a queue.
100+
Start receiving messages from a remote queue.
68101

69102
```js
70103
const msmq = require('updated-node-msmq');
71104

72-
var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
105+
var queue = msmq.connectToRemoteQueue('FormatName:DIRECT=OS:mobile-000000\\private$\\privatetest');
73106

74107
// Set receive listener callback
75108
queue.on('receive', (msg) => {
@@ -82,25 +115,31 @@ queue.startReceiving();
82115

83116
### Get all messages
84117

85-
Gets all messages without removing them from queue.
118+
Gets all messages without removing them from a remote queue.
86119

87120
```js
88121
const msmq = require('updated-node-msmq');
89122

90-
var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
123+
var queue = msmq.connectToRemoteQueue('.\\Private$\\MyAwesomeQueue');
91124
var messages = queue.getAllMessages();
92125
```
93126

94-
### Purge a queue
127+
#### Note:
128+
* Creating a queue / Checking if a queue exists on a remote machine is currently not supported by MSMQ.
129+
* To communicate with a remote queue, MSMQ should be enabled in the sender's machine too. Also, in the _Security_ tab of the queue on the remote machine should have the appropriate permissions set for _Everyone_ and _ANONYMOUS LOGON_.
130+
* The queue should already be created on the remote machine.
131+
* The format to connect to a remote queue is as follows:
132+
`
133+
msmsq.connectToRemoteQueue(path);
134+
`
135+
* `path` has to be in the following format:
95136

96-
Clears all messages from the queue.
137+
`FormatName:DIRECT=TCP:`_`<ip_address>`_`\\private$\\`_`<queue_name>`_`
97138

98-
```js
99-
const msmq = require('updated-node-msmq');
139+
or
140+
141+
`FormatName:DIRECT=OS:`_`<machine_name>`_`\\private$\\`_`<queue_name>`_`
100142

101-
var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
102-
queue.purge();
103-
```
104143

105144
## License
106145

0 commit comments

Comments
 (0)