Skip to content

Commit 493ac5b

Browse files
authored
Merge pull request #1 from joelmenezes/Access_Remote_Queues
Access remote queues
2 parents c25771b + 5d9e6c8 commit 493ac5b

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

MSMQLib/MSMQInterface.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ public async Task<object> CreateQueue(string path)
2626
return true;
2727
}
2828

29+
/// <summary>
30+
/// Sends a message to a remote MSMQ queue.
31+
/// </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)
35+
{
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));
45+
return true;
46+
}
47+
2948
/// <summary>
3049
/// Checks whether a queue exists or not.
3150
/// </summary>

proxy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ export var queueProxy = {
1616
send: getMethod('SendMessage'),
1717
receive: getMethod('ReceiveMessages'),
1818
list: getMethod('GetAllMessages'),
19-
clear: getMethod('PurgeQueue')
19+
clear: getMethod('PurgeQueue'),
20+
sendRemote: getMethod('SendToRemoteQueue')
2021
};

queue.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ 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);
37+
}
38+
3039
startReceiving() {
3140
if (this.receiving) {
3241
throw new Error('Already receiving messages from this queue');

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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.
1314

1415
## Install
1516

@@ -32,6 +33,35 @@ var queue = msmq.openOrCreateQueue('.\\Private$\\MyAwesomeQueue');
3233
queue.send('Hello from Node.JS!');
3334
```
3435

36+
### Send a message to a remote queue
37+
38+
Sends a message to a remote MSMQ queue.
39+
40+
```js
41+
const msmq = require('updated-node-msmq');
42+
43+
// Send message to a remote queue using hostname
44+
msmq.sendToRemoteQueue('FormatName:DIRECT=OS:mobile-000000\\private$\\privatetest', 'Hello from Node.JS!');
45+
46+
msmq.sendToRemoteQueue('FormatName:DIRECT=TCP:192.168.1.5\\private$\\privatetest', 'Hello again from Node.JS!');
47+
```
48+
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:
58+
59+
`FormatName:DIRECT=TCP:`_`<ip_address>`_`\\private$\\`_`<queue_name>`_`
60+
61+
or
62+
63+
`FormatName:DIRECT=OS:`_`<machine_name>`_`\\private$\\`_`<queue_name>`_`
64+
3565
### Receive messages
3666

3767
Start receiving messages from a queue.

0 commit comments

Comments
 (0)