i'm trying to createa realtime chat app with laravel 5.4, laravel-echo, redis and socket-io on homestead.
Please check my code below and then my problem
ChatConversation event:
class ChatConversation implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Chat $message, User $user)
{
$this->message = message;
//Người gửi
$this->user = $user
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel('chatroom');
}
}
My web.php:
Route::post('/privatechat/{id}/{receiver_id}', function(){
$user = Auth::user();
$room_id = request()->segment(5);
$rec = request()->segment(6);
$message = Chat::create([
'chat_room' => $room_id,
'message' => request()->get('message'),
'sender_id' => $user->id,
'receiver_id' => $rec
]);
broadcast(new ChatConversation($message, $user));
return ['status' => 'OK'];
})->middleware('auth');
My chat vue setting:
const app = new Vue({
el: '#app',
data: {
messages: [],
sendClass: 'chat-send'
//Điều kiện
},
methods: {
addMessage(message) {
this.messages.push(message);
axios.post(url_post, message);
/*.then(function (response) {
//swal('Tuyệt vời! Work like a charm!');
console.log(response);
})
.catch(function (error) {
console.log(error);
});*/
}
},
created() {
var u = url;
axios.get(u).then(response => {
//console.log(response);
this.messages = response.data;
});
/*Echo.join('chatroom')
.here()
.joining()
.leaving()
.listen('ChatConversation', (e) => {
console.log(e);
})*/
Echo.join('chatroom')
.listen('ChatConversation', (e) => {
console.log(e);
})
}
});
My bootstrap.js
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: '../socket.io-client/dist/socket.io',
host: window.location.hostname + ':6001'
});
My laravel-echo-server.json
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": ""
My first problem is when I use this script for accessing client library:
src="//{{ Request::getHost() }}:6001/socket.io/socket.io.js
I get this error GET http://hgc.develop:6001/socket.io/socket.io.js net::ERR_CONNECTION_REFUSED So I had to download socket from bower and change the line above to
<script src="{{ asset('js/socket.io-client/dist/socket.io.js') }}"></script>
My second problem is that when I go to my chat application page, the console show me these errors:
app.js:35246 TypeError: Cannot read property 'presenceChannel' of undefined
app.js:15520 Uncaught (in promise) TypeError: Cannot read property 'socketId' of undefined
One more, I follow this tutorial for creating my chat app: https://www.youtube.com/watch?v=8aTfMHg3V1Q but I use Redis and Socket.io instead of Pusher
What's wrong in my code? And how can I deal with these problems?