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

lara230832's avatar

Post request for every update websocket connection?

I am gathering data via a external websocket connection (via a Python script). For every update I receive I am making an Post request to my laravel api controller, where I process the data and put it in my database (via a job).

Beginner question: is this good practice (post request from python script) or would it be better if I use another method for this?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Making a POST request from a Python script to a Laravel API controller is a valid approach for sending data to the server. However, if you are receiving frequent updates, it may be more efficient to use a persistent connection like a WebSocket to avoid the overhead of establishing a new HTTP connection for each update.

To implement a WebSocket connection in Laravel, you can use a package like laravel-websockets. This package provides a WebSocket server that can handle incoming connections and broadcast messages to subscribed clients.

Here's an example of how you can use laravel-websockets to receive data from a Python script:

  1. Install the package via Composer:
composer require beyondcode/laravel-websockets
  1. Publish the configuration file:
php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="config"
  1. Start the WebSocket server:
php artisan websockets:serve
  1. In your Python script, use a WebSocket client library like websockets to connect to the server and send data:
import asyncio
import websockets

async def send_data():
    async with websockets.connect('ws://localhost:6001') as websocket:
        data = {'foo': 'bar'}
        await websocket.send(json.dumps(data))

asyncio.get_event_loop().run_until_complete(send_data())
  1. In your Laravel application, create a WebSocket event listener that receives the data and processes it:
use App\Events\NewDataReceived;

Broadcast::channel('data', function ($user) {
    return true;
});

Broadcast::listen('App\Events\NewDataReceived', function ($data) {
    // Process the data and put it in the database
});

class NewDataReceived implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function broadcastOn()
    {
        return new Channel('data');
    }
}
  1. In your Python script, broadcast the data to the server:
import asyncio
import websockets

async def send_data():
    async with websockets.connect('ws://localhost:6001') as websocket:
        data = {'foo': 'bar'}
        await websocket.send(json.dumps(data))
        await websocket.send(json.dumps({'event': 'NewDataReceived', 'data': data}))

asyncio.get_event_loop().run_until_complete(send_data())

This will broadcast the NewDataReceived event to all subscribed clients, including the Laravel application, which can then process the data and put it in the database.

1 like

Please or to participate in this conversation.