1
votes

I'm triyng to send some events from a Node Client to a Laravel echo server.

There's not so much documentation on internet. I tried to do it using socket.emit() but no events arrives to the echo-server.

Here's my app.ts code

import express = require('express');
import Echo from "laravel-echo";
import SocketIoClient from "laravel-echo";


// Create a new express application instance
const app: express.Application = express();
let io = require('socket.io-client');

let echo = new Echo({
  broadcaster: 'socket.io',
  host: 'http://3.16.169.253:6001',
  client: io
});


echo.channel('public').listen('drumdata', (e:any) => {
  console.log("DATA RECEIVED");
  console.log(e);
})

sendData();

async function sendData(){
for(let i=0;i<10;i++){
  await sleep(2000) 
  echo.connector.options.auth.headers['X-Socket-ID'] = echo.connector.socket.id
  echo.connector.socket.emit('drumdata', "public",{"eventData":"myData"}) ;

 }
}




function sleep(ms:number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

The Echo-Server log shows application joining the channel but no events is fired.

[3:25:25 PM] - S6s3SKH1L61C9U6NAAAr joined channel: public
[3:25:59 PM] - S6s3SKH1L61C9U6NAAAr left channel: public (transport close)
[3:26:08 PM] - qZljcx9BzDZOU-s8AAAs joined channel: public
[3:26:14 PM] - qZljcx9BzDZOU-s8AAAs left channel: public (transport close)
[3:27:02 PM] - 9--wTFOpWlZVIfw4AAAt joined channel: public

Does anyone have suggestions about it?

EDIT:

Running laravel-echo-server in DEBUG mode shows that the message is received but no event is fired to the channel. I even added 'X-Socket-ID' auth header.

2019-07-09T10:09:10.381Z socket.io:socket got packet {"type":2,"nsp":"/","data":["drumdata","public",{"eventData":"pincia"}]}
2019-07-09T10:09:10.381Z socket.io:socket emitting event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:10.381Z socket.io:socket dispatching an event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:12.350Z engine:ws received "42["drumdata","public",{"eventData":"pincia"}]"
2019-07-09T10:09:12.350Z engine:socket packet
2019-07-09T10:09:12.350Z socket.io-parser decoded 2["drumdata","public",{"eventData":"pincia"}] as {"type":2,"nsp":"/","data":["drumdata","public",{"eventData":"pincia"}]}
2019-07-09T10:09:12.350Z socket.io:socket got packet {"type":2,"nsp":"/","data":["drumdata","public",{"eventData":"pincia"}]}
2019-07-09T10:09:12.350Z socket.io:socket emitting event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:12.350Z socket.io:socket dispatching an event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:14.350Z engine:ws received "42["drumdata","public",{"eventData":"pincia"}]"
2019-07-09T10:09:14.350Z engine:socket packet

Thanks, Federico

1
looks like you only need to fire the event function?laravel.com/docs/5.8/broadcasting#broadcasting-events - 4m1r
My intention was to dispatch event from Node and not form laravel... - Federico Pinciaroli
So, if you're intention is to emit from the node process, you're gonna need an EventEmitter and you'll need to attach a function to an event which notifies the channel. You may even be able to pipe from the emitter to the channel, since they are both event streams. nodejs.org/api/events.html#events_events - 4m1r
you can use the http api of the laravel-echo-server to emit events: docs github.com/tlaverdure/laravel-echo-server#http - Roland Starke
facing same issue - Ashok Devatwal

1 Answers

0
votes

Okay, so I'm starting to see how this all works, the documentation is not super helpful, but it looks like the echo server uses an http api to post messages. In that case, it looks like they recommend axios. This solution should work, but I can't figure out what the path to the channel should be. Once you suss that out, you should be seeing your event propagated from the node server. Currently this just 404s.

const express = require('express');
const Echo = require('laravel-echo');
const io = require('socket.io-client');
const axios = require('axios');

// Create a new express application instance
const app = express();

const echo = new Echo({
  broadcaster: 'socket.io',
  host: 'http://127.0.0.1:6001',
  client: io
});


echo.channel('public').listen('drumdata', (e) => {
  console.log("DATA RECEIVED");
  console.log(e);
})


async function sendData(){
for(let i=0;i<10;i++){
  await sleep(2000);
  console.log('echoing'); // echo.socketId() is already set on X-Socket-Id thanks to Axios
  axios.post('http://127.0.0.1:6001/public/drumdata', {"eventData":"myData"})
    .then((response) => {
        console.log(response);
    });

 }
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

sendData();