I am trying to find the correct interfaces for published message and for received in HTTP cloud function/server
Here is my code for publish messages
import { Message, PubSub, Subscription, Topic } from '@google-cloud/pubsub'
import { MessageOptions } from '@google-cloud/pubsub/build/src/topic' // I don't like this import. I expect to import this type from '@google-cloud/pubsub'
export const instance = new PubSub()
export const psaWorkerTopic = instance.topic(
MAIN.PSA_WORKER_PUBSUB_TOPIC_NAME,
{
batching: PUBSUB_MAX_BATCHING,
messageOrdering: true,
}
)
export const publishPsaQueueMessage = async (
data: PubSubPsaQueue,
orderingKey?: string
) => {
try {
const dataBuffer = Buffer.from(JSON.stringify(data))
const message: MessageOptions = {
data: dataBuffer,
orderingKey,
}
const messageId = await psaWorkerTopic.publishMessage(message)
logger.log(
`PubSub::PSA::PUBLISHED::msgId: ${messageId}::batchId: ${data.batchId}::orderKey: ${orderingKey}`,
{
partnerId: data.partnerId,
orderingKey,
}
)
return messageId
} catch (e) {
logger.error('PubSub::PSA::Could not publish message: ', e)
if (orderingKey) psaWorkerTopic.resumePublishing(orderingKey)
throw e
}
}
here is the code for receiving messages
export const handleMessage = async (messageBody: {
message: {
data: string
messageId: string
publishTime: string
attributes?: Record<string, string>
orderingKey?: string
}
subscription: string
}): Promise<void> => {
const { message } = messageBody
const startTime = Date.now()
const payloadDataStr = Buffer.from(message.data, 'base64').toString('utf8')
if (!isValidJSONString(payloadDataStr)) {
throw new Error(`Invalid Json: ${payloadDataStr}`)
}
const payloadData: PubSubPsaQueue = JSON.parse(payloadDataStr)
logger.debug(
`PubSub::PSA::START::msgId: ${message.messageId}::batchId: ${payloadData.batchId}::orderKey: ${message.orderingKey}::time: ${message.publishTime}::Received payload`,
payloadDataStr
)
}
export const psaSubscriptionListener = () => {
psaWorkerSubscription.on('message', handleMessage)
psaWorkerSubscription.on('error', (err) => {
logger.error('PubSub::PSA::Unexpected error with GCP subscription:', err)
})
}
And now I see ts error on this line psaWorkerSubscription.on('message', handleMessage)
on handleMessage function
TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type
(messageBody: { message: { data: string; messageId: string; publishTime: string; attributes?: Record<string, string>; orderingKey?: string; }; subscription: string; }) => Promise<void>
is not assignable to parameter of type void
subscription.d.ts(68, 5): The last overload is declared here.
Here is example of real data which I receive on test server