feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
14.97k stars 744 forks source link

Client not able to listen to event #3315

Closed nickdex closed 8 months ago

nickdex commented 8 months ago

Title: Real-time socket connection not working between FeathersJS and ReactJS via socket.io

Description: Note - It is only a problem when listening to events i.e. on method. In network tab for ws connection, messages section we can see the request going and response coming for other methods (get, create etc). It also works inside the backend app but it is not sending to client

Steps to reproduce:

  1. I've set up a FeathersJS backend and a ReactJS frontend to establish a real-time socket connection.

  2. Backend Repository: FeathersJS Chat Trail

  3. Frontend Connection to FeathersJS:

    import { feathers } from '@feathersjs/feathers';
    import socketio from '@feathersjs/socketio-client';
    import io from 'socket.io-client';
    
    const socket = io("http://localhost:3030");
    const client = feathers();
    client.configure(socketio(socket));
    export default client;
  4. Chat.tsx Component:

    import { feathers } from '@feathersjs/feathers'
    import socketio from '@feathersjs/socketio-client'
    import io from 'socket.io-client'
    import React from 'react';
    
    const socket = io('http://localhost:3030')
    const client = feathers()
    // Set up Socket.io client with the socket
    client.configure(socketio(socket))
    
    const Temp = () => {
      const createMessage = async () => {
        const newRes = await client
          .service('message-3')
          .create({ text:"hello test message" });
        console.log('new object create', newRes);
      };
    
      client.service('message-3').on('created', data => {
        console.log('new message created i am from listener', data);
      });
    
      return (
        <div>
          <div>course create</div>
          <button onClick={createMessage}>createeee</button>
        </div>
      );
    }
    
    export default Temp;

Expected behavior:

Upon clicking the "createeee" button, I expect the console.log('new message created i am from listener', data) statement to be executed, indicating a successful real-time connection.

Actual behavior:

The listener on the frontend is not triggering the expected console log statement. However, the backend socket listener works correctly. Only the frontend listener seems unresponsive.

System configuration:

Module versions:

NodeJS version: 20.8.0

Operating System:

Browser Version:

React Native Version: N/A (web issue)

Module Loader: N/A

Additional Information:

We also tried out the feather-chat repository provided by the FeathersJS team. With both the quick start and the Feathers backend connected to the React chat app, we were able to successfully listen to events. However, the issue arises when using the Feathers CLI to generate the app and using it as is.

daffl commented 8 months ago

By default only authenticated users will receive events. A user needs to log in on the frontend as shown here for React.