Utilizing VUE with Socket.io or vue-socket.io Integration

I've been working on connecting using socket.io (client) and websocket.org (server) with vue.js. Despite going through all the examples, I can establish a connection to the socket but when I emit the event BOARD_ID, I don't receive any response. The socket server being used is wss://echo.websocket.org and I even tried a standalone example which worked perfectly.

main.js

import Vue from "vue";
import VueSocketIO from "vue-socket.io";
import router from "./router";
import App from "./App.vue";

Vue.config.productionTip = false;

Vue.use(
  new VueSocketIO({
    debug: true,
    connection: "wss://echo.websocket.org"
  })
);

new Vue({
  sockets: {
    connect: function () {
      console.log("Socket Connected");
    }
  },
  router,
  render: (h) => h(App)
}).$mount("#app");

Board.vue


<template>
  <div id="Board">
    <h3>Board ID :{{ this.$route.params.Bid }}</h3>
    <button v-on:click="goToHome">Go To Home</button>
    <button v-on:click="updateBoard">Send To Socket</button>
    <p>{{ message }}</p>
  </div>
</template>

<script>
export default {
  name: "Board",
  data() {
    return {
      domain: "https://someapp.app",
      path: "https://someapp.app/board/" + this.$route.params.Bid,
      message: "Not Recived",
    };
  },
  methods: {
    goToHome() {
      this.$router.push({ name: "Home" });
    },
    updateBoard: function (e) {
      this.$socket.emit("BOARD_ID", this.$route.params.Bid);
      console.log("Sent...");
    },
    listenBoard: function () {

      this.$socket.on("BOARD_ID", (data) => {
        console.log(data);
        this.message = "Received: " + data;
      });

      this.sockets.subscribe("BOARD_ID", (data) => {
        console.log(data);
        this.message = "Received: " + data;
      });

      console.log("Listening...");
    }
  },
  mounted: function () {
    this.listenBoard();
  },
};
</script>

Answer №1

Interacting with a Socket.IO client necessitates the presence of a dedicated Socket.IO server. Keep in mind that attempting to connect a Socket.IO client to the traditional WebSocket server, such as echo.websocket.org, will not yield desired results.

If your intention is to establish a connection with echo.websocket.org, you should explore utilizing the WebSocket API. However, if you are interested in solely using the echo server for testing purposes and wish to employ Socket.IO functionality, I suggest constructing your own Socket.IO server.

To gain further insights on what Socket.IO is not, refer to this link: Read more: What Socket.IO is not

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show different material ui TextFields based on what the user chooses

My dropdown menu offers two options: BUY x AND y AND z SAVE m BUY n FOR m If I select option 1, I want to display Textfields for x, y, and z. If I choose option 2, then only Textfields for n and m should be displayed for user input. In the code snippet ...

Ways to modify the hue of an li element in the context menu upon hovering

I'm currently working on a project that involves VueJS and Bootstrap. To enhance user experience, I've incorporated a context menu using the npm package called vue-context Vue Context Menu When a user hovers over an item on the context menu, the ...

The object continues to be undefined despite its placement in a global scope

Currently, I am working on building a game of pong and encountering an issue with creating a paddle in the initialize method. Despite storing it as a global variable, the paddle remains undefined. I even tried making it a property of 'window', bu ...

Launch a modal from a separate webpage

I've been trying to implement a modal using Bootstrap 4, and after referring to the documentation, it seems to be working smoothly. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <scri ...

Connecting Vue to external data sources

My goal is to utilize Vue as a thin layer to connect existing model objects to a view effortlessly. Below, there's a simple app that highlights my issue. I have a GainNode object from the Web Audio API, and I aim to link its value to a slider. In An ...

Looking for assistance with transferring API information to components

I am currently working on a basic Movie finder application that will display a list of movies containing the name entered by the user. My focus at this stage is to just show the information on the screen. As I'm using React for this project, I have t ...

Learn how to efficiently send multiple image files to the server by utilizing Formidable and React JS

I am encountering an issue while attempting to upload five images to the server. The code file is provided below, and any assistance would be greatly appreciated. In this scenario, I am inputting image files and storing them in an array, but Formidable in ...

How can we effectively manage error responses and retry a failed call in NodeJS without getting tangled in callback hell?

I am in search of an effective approach to handle the given situation. I am curious if employing promises would be a helpful solution? Situation Overview: When a call retrieves a callback, and this callback receives an error object as a parameter. My obj ...

Eliminate the CSS triggered by a mouse click

Having some difficulty toggling the switch to change the background color. Struggling with removing the applied CSS and getting it to toggle between two different colored backgrounds. Code Sample: <!Doctype html> <html lang="en"> <head> ...

The padding on the button inside the v-card is not being applied as expected

I am facing an issue with the following code snippet: <v-card height="200"> <v-card-actions class="mb-0"> <v-btn flat color="orange">Share</v-btn> <v-btn flat color="orange">Explore</v-btn> & ...

Draw rectangles in real-time on a canvas as the mouse is clicked and dragged

Trying to create a drawing program with JavaScript and the HTML canvas. Need help continuously drawing a circle at the mouse location. Code here: <canvas width = '450' height = '450' id = 'drawing'> </canvas> ...

Having trouble triggering a click event on Ant Design menu button using jest and enzyme

Troubleshooting the simulation of a click event on the Menu component using Antd v4.3.1 Component: import React from 'react' import PropTypes from 'prop-types' import { Menu } from 'antd' import { SMALL_ICONS, PATHS } fro ...

A clever JavaScript function generator encapsulated within an instant function nested within a jQuery ready statement

This code snippet features an immediate function enclosed within a jQuery ready function. $((function(_this) { return function() { document.write('called!'); }; })(this)); I am puzzled by where the resultant function ...

Identify when users reach the end of a webpage through scrolling using mousewheel actions and scroll events

I want to track when a user reaches the end of a page and tries to scroll further, even though there is no more content to see. One of my usability metrics includes identifying dead scrolls, so I need a reliable way to detect when users attempt to scroll ...

Encountered an Uncaught ChunkLoadError with Vercel Next.js: Chunk 0 failed to load

Upon removing the node modules and package-lock.json files, I encountered the error mentioned above when attempting to reload any page. The project works fine after being restarted for the first time. However, upon reloading the page again, it displays a b ...

Dynamic and static slugs in Next.js routing: how to navigate efficiently

I am facing a scenario where the URL contains a dynamic slug at its base to fetch data. However, I now require a static slug after the dynamic one to indicate a different page while still being able to access the base dynamic slug for information. For Ins ...

Updating a value based on a changed range slider in VueJS

My goal is to update the value once the Slider has been dragged. Below is the code I am currently using: <div id="app"> <vue-range-slider v-model="rangeSlider" v-bind="options"></vue-range-slider> <p class="mt-50">Value: { ...

References to high order functions in JavaScript

Although the code below is functional, I believe there might be a more efficient approach. I am currently passing a reference to the higher-order function's scope. var self=this; this.nodeModal.find(".modal-footer .save").click(function(){ ...

Modifying properties through JavaScript is not possible

I created an HTML form and I'm trying to change the attributes of a div and a button but for some reason, it's not working <form> <button type='button' id='other'>Sub</button> <select id="prop"> &l ...

Prevent anchor jumping caused by popstate event in Safari

This situation is really testing my patience. When navigating within the same page using anchors, the browser automatically takes you back/forward to the location of that link in your history when popstate is triggered. One might think that you could prev ...