Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

491
Vistas
webSocket.send() is not executed in react js?

I am working with WebSocket in reactjs, but WebSocket.send is not executing on the onClick event. here I create WebSocket

 const ws = new WebSocket("ws://192.168.18.112:8000/ws/notification/");

connect to WebSocket

 ws.onopen = (e) => {
    console.log("connect");
  };

here is my onClick function

  const sendNotification = (e) => {
    console.log("click");
    if (ws.readyState === WebSocket.OPEN && message !== "") {
      ws.send("message");
      console.log("Sending message"); // this console is execute
    } else if (ws.readyState === WebSocket.CONNECTING) {
      ws.addEventListener("open", () => sendNotification());
      console.log("Connecting state");
      console.log("Not Send");
    } else {
      console.log("nothing happen");
    }
  };

so here is my problem I am unable to find bug or problem in my code.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

try this W3CWebSocket. Also i don't think there si a "/" after notification after ws/notification double check if your end point correct or not.

mport React, { Component } from 'react';
import { w3cwebsocket as W3CWebSocket } from "websocket";

const client = new W3CWebSocket('ws://192.168.18.112:8000/ws/notification/');

class App extends Component {
  componentWillMount() {
    client.onopen = () => {
      console.log('WebSocket Client Connected');
    };
    client.onmessage = (message) => {
      console.log(message);
    };
  }
  
  render() {
    return (
      <div>
        Practical Intro To WebSockets.
      </div>
    );
  }
}

export default App;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda