0

I am trying to use socket.io. and want to send a variable from my javascript (client side) to laravel event and then pass it back to client. I commented in my code what I want.

below is my code:

Event:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class MarketUpdate implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets;

    public $data;
    public function __construct()
    {

        $this->data = [
            'abcd' => $id , /// I need to get this ID from client side.
        ];

    }

    public function broadcastOn()
    {
        return array('channel_test');
    }
}

Client JS:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
    <script>
var id = '9';
        var socket = io('http://localhost:8890');
        socket.on("channel_test:App\\Events\\EventNew", function (message) {
// I Want to send id here somewhere and display
            $('#abcd').html(message.data.abcd);
        });
    </script>
Haren Sarma
  • 2,267
  • 6
  • 43
  • 72

1 Answers1

0

As i understand, no one method, except making self processor/handler. I think, you need watch some like Ratchet. Native laravel build don't have methods to work with you messages. There has broadcasting system.

You send data from client with ajax then laravel send data to all subscribed clients with broadcast

Captain Fail
  • 77
  • 1
  • 3