Home

Issue: 900

How to create a websocket connection in my custom JS file and multiple components can use socket.send function

[issue link]

case 1:

custom JS file

var wsUrl = 'ws://localhost:8080';
var socket = new WebSocket(wsUrl);

Nuxt.js Error:

WebSocket is not defined

case2

custom JS file

if(typeof(window) !== 'undefined'){
    var wsUrl = 'ws://localhost:8080';
    var socket = new WebSocket(wsUrl);
 }

export {socket};

.vue file

import { socket } from '~assets/js/socket.js';
...
    mounted(){
        console.log(socket); //undefined
    }
...

chrome console:

undefined

How can I create websocket in custome Js file connection and can use in Vue file?

This question is available on Nuxt.js community (#c770)