'broadcast to others and dont broadcast to current user are not working

In my TaskList.vue I have the code below:

<template>
    <div>
        <ul>
            <li v-for="task in tasks" v-text="task"></li>
        </ul>

        <input type="text" v-model="newTask" @blur="addTask">
    </div>
</template>

<script>  
    export default {
        data(){
            return{
                tasks: [],
                newTask: ''
            }
        },

        created(){
            axios.get('tasks')
            .then(
                response => (this.tasks = response.data)
            );

            Echo.channel('task').listen('TaskCreated', (data) => {
                this.tasks.push(data.task.body);
            });
        },

        methods:{
            addTask(){
                axios.post('tasks', { body: this.newTask })

                this.tasks.push(this.newTask);

                this.newTask = '';
            }
        }
    }
</script>

When I hit the axios.post('tasks') end point, I got duplicate result in my current tab that i input the value and the another tab got only 1 value which is correct.

To avoid this, I tried to use broadcast(new TaskCreated($task))->toOthers();

OR

I put $this->dontBroadcastToCurrentUser() in the construct of my TaskCreated.php

However, both methods are not working. Any idea why?

The image below is from network tab of mine. One of it is pending, is that what caused the problem?

https://ibb.co/jpnsnx (sorry I couldn't post image as it needs more reputation)



Solution 1:[1]

I solved this issue on my Laravel Spark project by manually sending the X-Socket-Id with the axios post.

The documentation says the header is added automatically if you're using vue and axios, but for me (because of spark?) it was not.

Below is an example to show how I manually added the X-Socket-Id header to an axios post using the active socketId from Laravel Echo:

axios({
    method: 'post',
    url: '/api/post/' + this.post.id + '/comment',
    data: {
        body: this.body
    },
    headers: {
        "X-Socket-Id": Echo.socketId(),
    }
})

Solution 2:[2]

Laravel looks for the header X-Socket-ID when using $this->dontBroadcastToCurrentUser(); Through this ID, Laravel identifies which user (current user) to exclude from the event.

You can add an interceptor for requests in which you can add the id of your socket instance to the headers of each of your requests:

 /**
 * Register an Axios HTTP interceptor to add the X-Socket-ID header.
 */
Axios.interceptors.request.use((config) => {
  config.headers['X-Socket-ID'] = window.Echo.socketId() // Echo instance
  // the function socketId () returns the id of the socket connection
  return config
})

Solution 3:[3]

window.axios.defaults.headers.common['X-Socket-Id'] = window.Echo.socketId();

this one work for me..!!

Solution 4:[4]

Because protowall is not an index but the element itself. So you shouldn't try to reference wallArray0[protowall].x, but simply protowall.x.

Also for arrays you should be using a for..of loop instead of a for..in loop.

In other words try this:

for(const protowall of wallArray0){
    const testwall = new Wall(protowall.x, protowall.y);
    wallArray.push(testwall);
}

A more elegant solution would be

const wallArray = wallArray0.map(protowall => new Wall(protowall.x, protowall.y));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Giovanni S
Solution 2
Solution 3 Siddharth Joshi
Solution 4 Windom Earle