-
Notifications
You must be signed in to change notification settings - Fork 37
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
what about createChannel ? #19
Comments
amqp-ts was designed to make it easy to connect to amqp, without the need to create channels or wait for callbacks or promises to resolve before continuing. It uses the library you mention as a base. The code you give could be written as follows with amqp.ts in typescript: import * as Amqp from "amqp-ts";
// connect to the amqp server
var connection = new Amqp.Connection(configuration.amqpUrl);
// define a new queue
var queue = connection.declareQueue("", {exclusive: true});
// define the consumer for the queue
queue.activateConsumer( message => {
let msg = message.getContent();
if (msg.properties.correlationId == corr && msg.content != null) {
let resData = JSON.parse(msg.content);
res.json(resData);
}
}, {noAck: true});
// send a message to the queue
queue.send(new Message({ correlationId: corr, replyTo: q.queue });
// or, if you want to absolutely make sure that the queue is created before you send something
connection.completeConfiguration().then(() => {
queue.send(new Message({ correlationId: corr, replyTo: q.queue });
}).catch(err => {
console.log("An error occurred: " + err.message);
}); |
Oops, just noticed that I messed up. import * as Amqp from "amqp-ts";
// connect to the amqp server
var connection = new Amqp.Connection(configuration.amqpUrl);
// define a new queue
var queue = connection.declareQueue("", {exclusive: true});
let corr = uuid.v4();
// define the consumer for the queue
queue.activateConsumer( msg => {
let content = msg.getContent();
if (msg.properties.correlationId == corr && content != null) {
res.json(content); // I don't know what this line does
}
}, {noAck: true});
// send a message to the queue
queue.send(new Message(req, { correlationId: corr, replyTo: q.queue });
// or, if you want to absolutely make sure that the everything is initialized before you send something
connection.completeConfiguration().then(() => {
queue.send(new Message(req, { correlationId: corr, replyTo: q.queue });
}).catch(err => {
console.log("An error occurred: " + err.message);
}); |
Hello @abreits thanks for the help i'm referring RPC call on my implementation. i have microservice to consume messages on queue. but above implementation api request it self consume the request from queue. but i need something like this may be using this code it will possible
but problem was we cannot pass Message to rpc method (cannot pass correlationId, replyTo as properties) or i might understood this in wrong way. could you please help me to undestand. |
Do you mean that you have an existing rpc server and you need to write the client, or that you can both write the server and client (if the latter, then you can use the code in tutorial 6: https://github.com/abreits/amqp-ts/tree/master/tutorials/6_rpc If however you have an already existing server implementation then you need to do the following:
something like this: // connect to the reply_to queue
// amqp-ts does not work well with autogenerated queue names, so we generate a name
let responseQueueName = "rpc-response-" + uuid.v4();
let rpcResponseQueue = connection.declareQueue(responseQueueName, {exclusive: true});
//create a correlation id (if needed)
let corr = uuid.v4();
// define the consumer function (can also be done inline)
function rpcConsumer (msg: Amqp.Message) {
rpcResponse = msg.getContent();
// process rpc response, e.g.
if (msg.properties.correlationId == corr && rpcResponse != null) {
res.json(rpcResponse);
}
}
}
// connect to consumer to the queue
rpcResponseQueue.activateConsumer(rpcConsumer, {noAck: true});
// connect to the rpc queue
let rpcQueue = connection.declareQueue(rpcQueueName);
// send a message to the rpc queue
let rpcRequest = new Amqp.Message(rpcRequestContent, {correlation_id: corr, reply_to: responseQueueName});
rpcQueue.send(rpcRequest); |
Thanks you very much!! That did the job!! thanks again.. |
Sorry for the bothering is there any specific reason to use |
No reason, I just added it because it was present in your original example. If it causes problems just remove it. |
when consuming message i'm getting error
i thought it because of queue option but something else |
According to the error something went wrong in your consumer function, try to debug it there. |
i have dig in to source code and realized i'm calling this function to initialize consumer
but when i call but in source this line it again try to create message |
That is not a problem, a new Message can be created with a Buffer, it will then be used as is. see how the content of a new Message object is created: |
i'm trying to convert native this code using amqp-ts
but createChannel , assertQueue not implemented or not there ?
http://www.squaremobius.net/amqp.node/channel_api.html#model_createChannel
The text was updated successfully, but these errors were encountered: