Optimizing Event Payloads: Lessons Learned from Pusher Production Limits

Dec 26, 2023 / 1 min read

We are passing bunch of objects (json/array) that are not wrong but not necessary when passing information between events and listeners. This could lead to issues as

Our pusher production instannce on a project was limiting the size of json payload they can receive. In other words, when we emit an event with a huge payload, the event would be dismiss as not received thus breaking all listeners code waiting for that particular event. That wasn't still the worse part about it.

As any project, we had different environments (dev, stage, prod). Everything was working fine on lower environments but not on prod. Nothing would point at a defect in the environment setup itself.

Many hours later and 5+ people involved, we were able to fix the issue by switching all our events from eloquent model to primary key.

class MyEvent {
-  public function __construct(protected User $user) {
+  public function __construct(proctected int $userId) {
	}
}
class MyEventListener {
  public function handle(MyEvent $event) {
-    $user = $event->user;
+    $user = User::findOrFail($event->userId);
	}
}