Search code examples
javascriptlaravel-5.4getstream-io

getstream laravel with streamjs live notification


I can't seem to make the live notification work. I have already implemented on my project the feeds. but i need to implement LIVE notifications whenever someone follows, likes, or comments on my post. i also implmeneted the notifications page (where user can check previous notifications).

<script type="text/javascript">
var feed = client.feed('notification', '1', '1999');
function updateNotificationCallback(data) {
    alert(data);
}
feed.subscribe(function callback(data) {
    updateNotificationCallback(data);
});
</script>

I am using the code above, which i got from the streamjs repo docs. I am assuming that the code above is the one that will check if current user has some new notification? there are no errors, but i am assuming when someone follows me, the updateNotificationCallback will be called.

Am i missing something?

for the moment, i am using code below to notify current user if he has some unread notifications.notification toolbar

when user clicks on the notification icon, it will redirect him to the notifications page, which will also set all retrieved notificatiions to "read" thus resetting the counter.

HomeController.php

public function myNewNotifications() {

  $new = 0;

  try {
    $notification_feed = FeedManager::getNotificationFeed(Auth::user()->id)->getActivities()['unread'];
    $new = (int)$notification_feed;
  } catch(ErrorException $e) {
    $new = 0;
  }

  return response()->json([
    'new' => (int)$new
  ]);
}

VueJs component to retrieve unread notification count:

<template>
<span class="badge notificationCounter" v-if="notificationsCount >= 1">{{ notificationsCount }}</span>
</template>
<script>
  export default {
    mounted() {
      this.notificationsCount = this.initialCount;
      this.loadData();
      var that = this;
      setInterval(function () {
        that.loadData();
      }, 30000);
    },
    data() {
      return {
        notificationsCount: 0
      }
    },
    props: [
      'initialCount'
    ],
    methods: {
      loadData: function () {
        axios.get('/my/activities/notifications/new').then((response) => {
          this.notificationsCount = response.data.new;
        });
      }
    }
  }
</script>

Solution

  • Realtime updates are only fired when an activity is added or removed to a feed.

    If you want to send notifications for follow/unfollows the simplest approach is to create an activity for it (eg. Tommaso follows Rayzor) and send it to the notification feed of the followed user.

    If you use the Laravel integration, you can do this by adding the Activity trait to your Follow model.