Search code examples
phplaravelreact-nativeredislaravel-echo

React Native with Laravel Echo and Socket.io no response


I am building a basic app where I want to get the events data in real-time from Laravel Echo. I am using stack.io. Everything works just fine with laravel redis and everything running and working with no issues. However, I am not getting a console log for my event from react native and there is no warnings or errors. Here is my code

React Native App.js

import React from 'react';
import {
  StyleSheet,
  Text,
} from 'react-native';

import Echo from 'laravel-echo';
import socketio from 'socket.io-client';

const echo = new Echo({
  host: 'projectb.io:6001', //tried 127.0.0.1, localhost, ws:// etc...
  broadcaster: 'socket.io',
  client: socketio,
});

echo
    .channel('home')
    .listen('NewMessage', ev => console.log(ev));

const App: () => React$Node = () => {
  return (
    <>
      <Text>The usual stuff</Text>
    </>
  );
};

const styles = StyleSheet.create({

});

export default App;

Laravel Root, laravel-echo-server.json:

{
    "authHost": "http://localhost",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}

NewMessage event:

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

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('home');
    }
}

.env:

BROADCAST_DRIVER=redis
QUEUE_CONNECTION=redis

I am using laravel tinker to simply do a event(new App\Event\NewMessage("Hello Stack Overflow")). It does show on my queues and on my laravel echo server instantly but on my react native, I am not getting any console logs.

I am using 'RedisManager' => Illuminate\Support\Facades\Redis::class, instead of 'Redis' => Illuminate\Support\Facades\Redis::class, because there was an error stating that redis ext was not found. And now REDIS_CLIENT is set to predis.


Solution

  • Resolved. When I put the host IP as the server IP Address, it works fine. Hope this helps someone