Home > Enterprise >  Listen to websocket outside of my Laravel Application
Listen to websocket outside of my Laravel Application

Time:11-24

My application is a VueJS frontend and a Laravel Backend API. I want to subscribe to Binance API WebSocket but I'm unsure how to do it.

Any ideas? I'm talking about the high level design of it. I was not able to find a way to listen to a WebSocket from the backend in Laravel.

  • FYI, I've already setup a websocket server and I'm sending events through it so my Vue JS Front end knows when the specific event happens, now I need to actually take the event from Binance WebSocket. It's important for me to listen to the websocket from the backend since that's where the action should happen.

For sure this has to run somewhere at the back in some kind of isolated env or ?

CodePudding user response:

I don't understand exactly what you want, but... e.g:

Method 1: Using Laravel Echo Server

Laravel Echo Server is a Node.js server that allows you to listen to WebSocket connections outside of your Laravel application. You can install Laravel Echo Server using the following command:

(https://github.com/tlaverdure/laravel-echo-server)

npm install -g laravel-echo-server

After installing Laravel Echo Server, you can start it using the following command:

laravel-echo-server start

You can also configure Laravel Echo Server using the following command:

laravel-echo-server init

You can find more information about Laravel Echo Server here:

Method 2: Using Pusher

Pusher is a service that allows you to listen to WebSocket connections outside of your Laravel application. You can find more information about Pusher here:

https://pusher.com/

Method 3: Using Redis

Redis is a service that allows you to listen to WebSocket connections outside of your Laravel application. You can find more information about Redis here:

https://redis.io/

Method 4: Using Socket.io

Socket.io is a service that allows you to listen to WebSocket connections outside of your Laravel application. You can find more information about Socket.io here:

https://socket.io/

Did you want such an example?

CodePudding user response:

I also faced the similar issue recently, I found a solution which is, you can use a package called vue-pusher, by using it you can easily listen to any event, the uses are like this:

first import the package and set the params

Vue.use(require('vue-pusher'), {
api_key: process.env.MIX_PUSHER_APP_KEY,
options: {
    cluster: 'ap2',
    encrypted: true,
}
});

then in the template use it like this:

import Pusher from 'pusher-js'

this.$pusher.subscribe('YOUR_EVENT_NAME')
  this.$pusher.bind('your_variable', data => {
    
    let tempdata = JSON.stringify(data);
    let newdata = JSON.parse(tempdata);
    this.messages.push(newdata.message);
    console.log('wokring');
  })

I used it for live chat so i just push the new message in the messages array, here you can do your stuff.

  • Related