Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Real time application with Laravel / VueJS

Hello,

I have a real time application with laravel / VueJS which communicates with RabbitMQ.

The application receives a lot of messages each second (sometimes up to more than 20 and when it will be in production, probably up to several hundred).

For each message I need to refresh the screen and according to me it's not a solution to refresh the screen 100 times per second. I already notice that with 20 or 30 refreshes per second, the application is less reactive on the screen.

I thought about refreshing the screen only once per second or once each 5 seconds for example and not for each message received from RabbitMQ. But it's not the best solution.

I specify that it's the first time that I develop a real time application.

What do you suggest me to improve the reactivity ?

Thanks for your help.

V

0 likes
4 replies
LaryAI's avatar
Level 58

To handle a high volume of real-time messages in a Laravel/VueJS application while maintaining good performance and user experience, you can implement a strategy to batch and throttle updates. Here’s a step-by-step solution:

  1. Batching Messages: Instead of updating the UI for every single message, you can collect messages in a batch and update the UI at regular intervals.

  2. Throttling Updates: Use a throttling mechanism to limit the frequency of UI updates.

  3. Efficient State Management: Ensure that your state management in VueJS is optimized to handle frequent updates efficiently.

Here’s a detailed implementation:

Step 1: Set Up a Message Queue in VueJS

Create a message queue in your Vue component to store incoming messages temporarily.

data() {
  return {
    messageQueue: [],
    lastUpdate: Date.now(),
    updateInterval: 1000 // Update every second
  };
},

Step 2: Handle Incoming Messages

When a message is received from RabbitMQ, push it to the message queue.

methods: {
  handleMessage(message) {
    this.messageQueue.push(message);
    this.throttleUpdate();
  },
  throttleUpdate() {
    const now = Date.now();
    if (now - this.lastUpdate >= this.updateInterval) {
      this.updateUI();
      this.lastUpdate = now;
    }
  },
  updateUI() {
    // Process the message queue and update the UI
    this.processMessages(this.messageQueue);
    this.messageQueue = [];
  },
  processMessages(messages) {
    // Implement your logic to process and display messages
    console.log('Processing messages:', messages);
  }
}

Step 3: Integrate with RabbitMQ

Ensure that your RabbitMQ consumer calls the handleMessage method whenever a new message is received.

// Example using a WebSocket or any other RabbitMQ client library
const socket = new WebSocket('ws://your-rabbitmq-server');

socket.onmessage = (event) => {
  const message = JSON.parse(event.data);
  this.handleMessage(message);
};

Step 4: Optimize VueJS State Management

Ensure that your VueJS state management is optimized. For example, if you are using Vuex, make sure mutations and actions are efficient.

// Example Vuex store
const store = new Vuex.Store({
  state: {
    messages: []
  },
  mutations: {
    addMessages(state, messages) {
      state.messages.push(...messages);
    }
  },
  actions: {
    processMessages({ commit }, messages) {
      commit('addMessages', messages);
    }
  }
});

Step 5: Update the UI Efficiently

Use VueJS's reactivity system to update the UI efficiently. For example, use computed properties and watchers to react to state changes.

computed: {
  displayedMessages() {
    return this.$store.state.messages;
  }
},
watch: {
  displayedMessages(newMessages) {
    // Update the UI with new messages
    console.log('New messages to display:', newMessages);
  }
}

Conclusion

By batching messages and throttling updates, you can significantly improve the performance and reactivity of your real-time application. This approach ensures that the UI is updated at a manageable rate, providing a smoother user experience even under high message loads.

1 like
vincent15000's avatar

Lary's suggestions seem to be interesting.

But I'd like to have also some shared experience from real persons.

What do you think about Lary's suggestions ?

Do you have any other suggestions for me ?

I specify that it's the first time that I develop a real time application.

Thanks a lot for your help.

V

martinbean's avatar
Level 80

@vincent15000 Well what is it you’re actually trying to display? Why do you need to do full page refreshes for messages coming in? What do these messages contain?

No one can really tell you the best solution without knowing any real context about the problem at hand.

2 likes
vincent15000's avatar

@martinbean Ok I understand ... well ... I don't need a full page refresh.

The messages generate some updates in the dabatase and I need to send a request to the backend to retrieve the datas and update them on the screen, the updates are only some text to display.

Please or to participate in this conversation.