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

saurav77's avatar

Can i use Laravel Socket for external API Project ?

I am two projects one in laravel and the other one is a simple HTML page. I want to build a chart on the HTML page from data from the project of laravel. So I created an API route, build a query for the chart, and fetch the data in an external API project (HTML page) using google Chart. So everything is working fine except I need to real-time chart so data in the chart can be fetched without refreshing or reloading the page. So Can you give a suggestion or any ideas How should I do it? .Do I need to use a socket in both laravel and External API projects? Does laravel socket work both on laravel and external API Projects?

0 likes
4 replies
Kinomej's avatar
Kinomej
Best Answer
Level 11

Hello @saurav77,

you could use websockets to do this. Websockets are easily integrated with Laravel Echo. https://laracasts.com/series/get-real-with-laravel-echo

https://github.com/laravel/echo

https://beyondco.de/docs/laravel-websockets/getting-started/introduction

Alternativly you could just use polling https://freek.dev/1036-long-poll-requests-with-laravel.

js Example https://gist.github.com/twmbx/2321921670c7e95f6fad164fbdf3170e

The diffrence is: Websockets have a continuous connection to your server. You could update the chart exactly when the data changed. This would be the real real-time experience.

Polling just sends an requests every few seconds to retrieve the data you need. The chart would just be updated every few seconds (or every second, depends on your settings) :D

1 like
Kinomej's avatar

That could be a solution, yes. :D

Do you need to use websockets?

If not use polling instead. Here is a little example:

// Your Frontend Laravel only:
$(document).ready(function () {
  function poll () {
    $.get({
      url: 'https://your.domain/your/api/route',
      success: function (data) {
         if(data.statusCode)
             {
                window.location = "your/page/site"; // reload page
             }
      },
      timeout: 10000                    // == 10 seconds timeout
    }).always(function () {
      setTimeout(poll, 30000)           // == 30 seconds polling period
    })
  }

  // start polling
  poll()
})
			

If you want to use PHP Laravel stuff only (without a fancy js framework) you could use Livewire to avoid refreshing the page

https://laravel-livewire.com/

Example for a js framework (react)

componentDidMount() {
    this.timer = setInterval(()=> this.getChartData(), 1000);
  }

  componentWillUnmount() {
    this.timer = null;
  }

  getChartData() {
    fetch('https://your.domain/your/api/endpoint"))
        .then(result => result.json())
        .then(result => this.setState({ chartItems: result }));
  }
Kennith45's avatar

The initial Laravel Websockets release is a collaborative effort by Marcel Pociot and Freek Van der Herten. Freek has a very detailed writeup Introducing laravel-websockets, an easy to use WebSocket server implemented in PHP that goes into great detail on the background of the package, how to use it, and many other aspects of the projects.

The documentation includes some benchmarks and the scale looks promising. Of course, your mileage will vary from project-to-project, including your specific application needs and usage patterns.

To learn more about the package and how to use it to check out the Laravel WebSockets documentation. The source code is available on GitHub at beyondcode/laravel-websockets. https://laravel-news.com/laravel-websockets https://www.quickpay-portalinfo.com/

Please or to participate in this conversation.