Hi !
I'm facing an issue using the v1 PublisherClient: when I publish an array of messages as described in the documentation (https://cloud.google.com/nodejs/docs/reference/pubsub/0.20.x/v1.PublisherClient#publish), the custom attributes are empty.
Below is my code:
The message object that is published is built according to the documentation I found here: https://cloud.google.com/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.PubsubMessage.
Publisher:
```
const data = {key: 'data'};
const customAttributes = {attributeKey: 'attributeData'};
const topic = 'myTopic';
const project = 'myProject';
const pubsubMessages = [];
const messageBuffer = Buffer.from(JSON.stringify(data));
pubsubMessages.push({
data: messageBuffer,
attributes: customAttributes,
});
const publisher = new pubsub.v1.PublisherClient();
const formattedTopic = publisher.topicPath(project, topic);
const request = {
topic: formattedTopic,
messages: pubsubMessages,
};
publisher.publish(request).then(() => { // Do something });
**Subscriber**:
const project = 'myProject';
const subscription = 'mySubscription';
const subscriber = new pubsub.v1.SubscriberClient();
const subscriptionPath = subscriber.subscriptionPath(
project,
subscription
);
const request = {
subscription: subscriptionPath,
maxMessages: 5,
};
subscriber.pull(request).then(([response]) => {
const messages = response.receivedMessages;
messages.forEach(message => {
const messageData = JSON.parse(message.message.data.toString());
const messageAttributes = message.message.attributes; // attributes is an empty object
});
```
@google-cloud/pubsub version: 0.21.1Thanks!
@watsab your call to the publisher looks correct to me. By any chance have you tried using the hand written publisher?
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('myTopic');
const publisher = topic.publisher();
const data = Buffer.from(JSON.stringify({key: 'data'}));
const attributes = {attributeKey: 'attributeData'};
publisher.publish(data, attributes).then(() => {});
Hi @callmehiphop !
Again thanks for your answer :wink:
Using the hand written publisher works fine. However as I need to publish several messages at a time, I would have preferred to call the publish method once rather than looping through each and calling the "publish" method several times. And I was hoping it could be done using the the v1.Publisher, especially si I have just one promise to handle and not serveral. Yet if it is not possible, I'll arrange my code so it uses the hand written publisher.
Thanks anyway !
Hi !
I actually don't know what I've done but it seems attributes are now added to message and successfully published. Again thanks @callmehiphop !
Just a warning if some encounter the same problem: custom attributes can only be a 1-level object, meaning that it cannot be something like:
const attributes = {
key: 'value',
key1: {
key11: 'value'
}
}
but rather:
const attributes = {
key: 'value',
key1: "{key11: 'value'}"
}
Glad you got it worked out, thanks for coming back to let us know what your fix was!
Most helpful comment
Hi !
I actually don't know what I've done but it seems attributes are now added to message and successfully published. Again thanks @callmehiphop !
Just a warning if some encounter the same problem: custom attributes can only be a 1-level object, meaning that it cannot be something like:
const attributes = { key: 'value', key1: { key11: 'value' } }but rather:
const attributes = { key: 'value', key1: "{key11: 'value'}" }