jeudi 15 mars 2018

Chat message with Socket.io

I want to move chat from pusher to socket.io. My previous Chat is working perfect with Pusher but now i am moving to socket.io. I Have tried to change the pusher brodcast to redis in my .env. I ave also tried test chat with Node server.js it also works but i failed to connect with app.js

here is my bootstrap.js code

import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.io = require('socket.io-client');

/*window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'mypusherkey',
    cluster: 'us2',
    encrypted: true
});*/ **I have commented this to move on socket.io**

 window.Echo = new Echo({
     broadcaster: 'socket.io',
     host: window.location.hostname + ':8080'
 });

And my app.js

const livechat = new Vue({
el: '#livechat',
data: {
    conversation: [],
    usersInRoom: [],
    status: 'offline',
    seen: 1,
},
methods: {
    addMessage(message) {
        // Add to existing messages
        //this.conversation.messages.push(message);

        // Persist to the database etc
        axios.post('/messages', message).then(response => {
            this.conversation.messages.push(response.data);
        })
    }        
},
created() {
    axios.get('/messages/' + user_id).then(response => {
        this.conversation = response.data;
        Echo.channel('chat.' + this.conversation.conversation_id)
            .listen('MessageSent', (e) => {
                console.log(e);
            }); //I have tried to check this code but i m not receving any thing in console log

        // Echo.join('chat.' + this.conversation.conversation_id)

            // .here((users) => {
                // this.usersInRoom = users;
            // })
            // .joining((user) => {
                // this.status = (user.id === this.conversation.withuser) ? 'online' : 'offline';
                // this.usersInRoom.push(user);
            // })
            // .leaving((user) => {
                // this.usersInRoom = this.usersInRoom.filter(u => u !== user)
            // })

            // .listen('MessageSent', (e) => {
                // this.conversation.messages.push({
                    // conversation_id: e.message.conversation_id,
                    // message: e.message.message,
                    // name: e.user.firstname
                // });
            // });
    });


}
});

laravel-echo-server.js

{
"authHost": "http://MY_AP",
"authEndpoint": "/broadcasting/auth",
"clients": [
    {
        "appId": "5*************",
        "key": "f*********************"
    }
],
"database": "redis",
"databaseConfig": {
    "redis": {
        "port": "6379",
        "host": "localhost"
    },
    "sqlite": {
        "databasePath": "/database/laravel-echo-server.sqlite"
    }
},
"devMode": true,
"host": null,
"port": "8080",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
    "allowCors": true,
    "allowOrigin": "http://MY_IP:8080",
    "allowMethods": "GET, POST",
    "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}

I have tried to add server.js code in app.js before const chat but it fails

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(8080);

Note: This complete code is placed in aws ubuntu so i have installed every thing related to chat redis-cli is working. node installed nginx and apache2 is working.

And the 2nd issue when I am runing throught server.js I am not receiving any error but when i run with app.js it gives http://MY_IP:8080/socket.io/?EIO=3&transport=polling&t=M8feALs net::ERR_CONNECTION_TIMED_OUT

Please help me out. thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire