feathersjs / feathers

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

Socket io and REST connection using feathers v4.5.x #3383

Closed Venu171 closed 8 months ago

Venu171 commented 8 months ago

How to use socket io when I have the rest implemented in frontend and configured in my feathers, but when I am using the socket io connection and getting configured to the same feathers It is stating that only one default is allowed. I am using React here and also provide me a guide so that i can learn the feathers js like pdf, etc; pls do this help....

Can I use code in this format: import React, { useEffect } from 'react'; import { feathers } from '@feathersjs/feathers'; import rest from '@feathersjs/rest-client'; import socketio from '@feathersjs/socketio-client'; import io from 'socket.io-client';

const MyComponent = () => { useEffect(() => { // Create a Feathers app instance const app = feathers();

// Configure the REST client
const restClient = rest('http://api.example.com'); // Replace with your API URL
app.configure(restClient.fetch(window.fetch));

// Create a Socket.io client instance
const socket = io('http://api.example.com'); // Replace with your Socket.io server URL

// Configure the Socket.io client
app.configure(socketio(socket));

// Example: Call a Feathers service using the REST client
app.service('messages').find().then(response => {
  console.log('REST response:', response);
});

// Example: Real-time events using Socket.io
app.service('messages').on('created', message => {
  console.log('New message created:', message);
});

// Cleanup on component unmount
return () => {
  // Disconnect from Socket.io when component unmounts
  socket.disconnect();
};

}, []); // Run this effect only once on component mount

return (

{/* Your component JSX */}

); };

export default MyComponent;

Venu171 commented 8 months ago

Please do respond for this.