Reputation: 2996
I am using a Node.js server to talk to a python DAO with Amazon SQS. I'm able to send a SQS to the DAO but have no idea on how to send something back on the python DAO and listen for it properly on the Node.js server. I'm also curious how to call another function based off what the Node.js SQS message is received from the python DAO. Any help would be greatly appreciated! Here's my code so far:
Node.js listening for SQS code:
app.get('/readDAOSQSMessage', function(req, res) {
var params = {
QueueUrl: DAO_QUEUE_URL,
MaxNumberOfMessages: 1, // how many messages do we wanna retrieve?
VisibilityTimeout: 60, // seconds - how long we want a lock on this job
WaitTimeSeconds: 3 // seconds - how long should we wait for a message?
};
sqs.receiveMessage(params, function(err, data) {
if (data.Messages) {
var message = data.Messages[0],
body = JSON.parse(message.Body);
res.send("Username: " + body.username + " Password: " + body.password);
removeFromQueue(message, DAO_QUEUE_URL);
}
});
});
Python sending SQS code: queue = conn.get_queue('DAO-Queue')
writeQueue = conn.get_queue('DaoToServerQueue')
message = RawMessage()
message.set_body("This is my first message.")
writeQueue.write(message)
while True:
rs = queue.get_messages()
if len(rs) > 0:
m = rs[0]
print("Message Body: " + m.get_body())
message = m.get_body()
#unicodedata.normalize('NFKD',message).encode('ascii','ignore')
#command = 'dao.'+message[1:-1]
message = message.encode('ascii','ignore')
print "message: "+message[1:-1]
print "backslash: "+'\\'
replace_str = '\\'+'"'
print replace_str
print message.find(replace_str)
print"\nmessage type: "+str(type(message))
message = message.replace(replace_str,'"')
print "\nnew message: "+message
command = 'dao.'+message[1:-1]
print "\n command: "+command
#unicodedata.normalize('NFDK',message).encode('ascii','ignore')
#command = 'dao.'+ message
#unicodedata.normalize('NFDK',command).encode('ascii','ignore')
eval(command)
print("Command: " + command)
queue.delete_message(m)
Upvotes: 0
Views: 1056
Reputation: 1942
Normally you want to listen to messages on the receiving side by long-polling.
function readMessages() {
async.parallel([
pollMessage,
], function(err, callbacks) {
readMessages();
});
}
function pollMessage(callback) {
sqs.receiveMessage({
QueueUrl: receiveQueue,
WaitTimeSeconds: 5,
MaxNumberOfMessages: 1,
// VisibilityTimeout: 20
}, function(err, data) {
if (err) console.log(err);
if (!data) {
callback();
return;
}
if (data.Messages) {
// Get the first message (should be the only one since we said to only get one above)
for (var i = 0; i < data.Messages.length; i++) {
var message = data.Messages[i];
removeFromQueue(message, callback);
// Now this is where you'd do something with this message
handleIncomeMessage(message); // whatever you wanna do
// Clean up after yourself... delete this message from the queue, so it's not executed again
}
} else {
callback();
}
});
}
The code to be executed as lambda function on your NodeJS server. You can decide what to do with the message by attaching message type to the payload. Notice the handleIncomeMessage() function above, which can be something like:
function handleIncomeMessage(data) {
var msg = JSON.parse(data.Body);
var type = msg.type;
console.log("Got a new sqs message: ", msg.type);
switch (type) {
case 'someCoolType':
{
Manager.proceedWithCoolType(msg.restOfPayload);
break;
}
}
}
Upvotes: 1