Static GitHub Issues

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

prev: 360 browser compatibility mode auto redirect
next: nuxt 1.x version not working performance: { prefetch: false }

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?

<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This question is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c770">#c770</a>)</em></sub></div>