Description
We are using "pusher": "^5.1.3" in our RealtimeService and are occasionally experiencing an error when triggering events. This is a PusherRequestError with status code 400, which suggests that the request made by the pusher library to the Pusher API is failing.
Here's the complete error stack trace:
Error:
at new RequestError (/app/packages/server/node_modules/pusher/lib/errors.js:13:16)
at /app/packages/server/node_modules/pusher/lib/requests.js:61:17
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at RealtimeService.publish (/app/packages/server/src/core/services/realtime/realtime.service.ts:30:13)
at AssetService.updateQueueProgress (/app/packages/server/src/modules/asset/asset.service.ts:415:9)
at AssetService.setCompleted (/app/packages/server/src/modules/asset/asset.service.ts:380:23)
at AssetWebhookHandler.handleAssetProcessingComplete (/app/packages/server/src/modules/asset/webhooks/webhook.handler.ts:827:29)
at AssetWebhookResolver.handleAssetProcessingComplete (/app/packages/server/src/modules/asset/webhooks/webhook.resolver.ts:430:13) {
name: 'PusherRequestError',
message: 'Unexpected status code 400',
url: 'http://api-eu.pusher.com/apps/1614183/events?auth_key=APP_KEY&auth_timestamp=1689415586&auth_version=1.0&body_md5=3cfafa3985dfd22ca3fa4a08ccbe2621&auth_signature=ef83ac3eb82a3c553333387f8cfe8d9e858caf9cde41a11b99f7ab942e56df7b',
error: undefined,
status: 400,
body: 'Invalid JSON provided (could not parse)\n'
}
This error occurs sporadically and it doesn't appear to be related to the payload being sent with the trigger. We've also tried reinstalling the pusher package, but this has not resolved the issue.
Here is the relevant method in the RealtimeService where we trigger the events:
class RealtimeService {
private pusher: Pusher;
constructor() {
const {
PUSHER_APP_ID,
PUSHER_APP_KEY,
PUSHER_APP_SECRET,
PUSHER_APP_CLUSTER,
} = process.env;
this.pusher = new Pusher({
appId: PUSHER_APP_ID,
key: PUSHER_APP_KEY,
secret: PUSHER_APP_SECRET,
cluster: PUSHER_APP_CLUSTER,
useTLS: process.env.NODE_ENV as string !== 'local',
});
}
public async publish<E extends Events>(channel: Channels, userId: string, event: E, data: EventPayload[E]) {
try {
console.log(`Publishing ${event} to ${channel}.${userId}`, data);
await this.pusher.trigger(`${channel}.${userId}`, event, data);
} catch (e) {
console.log(e);
}
}
}
export default RealtimeService;
And this is an example of a payload being sent:
Publishing UPLOAD_PROGRESS to NOTIFICATIONS.4d63e01f-82f8-4584-9e4b-7708e04764dc { failed: 0, completed: 5, processing: 1, queued: 1, total: 7 }
Publishing JOB_COMPLETED to MIGRATIONS.ONLY_ADMINS { jobId: '08903cd9-1237-4fe4-8d58-2997dbb5b388' }
Any help or insights into why this may be happening would be much appreciated.