I am using react hook to create application, and socket.io-client for communication between flask and react, trying to access socket.io-client instance in different components of the application,
Please check below code, inside socket-Instance.js, created instance of socketio after connection and stored in store.js using dispatch,
in device.js, trying to access stored socket instance, and emit data on toggle,
But in socket-instance, i am trying to print console.log(socketInstance); this is coming as null
i wanted to create socketinstance once, and trying to emit the data from different components, using connected socket
using https://www.npmjs.com/package/socket.io-client this library
Index.js
import { StateProvider } from './shared/store';
const app = (
<StateProvider>
<AppRouting />
<SocketInstance />
</StateProvider>
);
store.js
import React, { createContext, useReducer } from 'react';
const initialState = {
deviceStatus: []
};
const store = createContext(initialState);
const { Provider } = store;
const StateProvider = ({ children }) => {
const [state, dispatch] = useReducer((currentState, action) => {
const newState = { ...currentState };
console.log(action.type);
switch (action.type) {
case 'socket':
newState.websocket = action.currentState
return newState
default:
throw new Error();
};
}, initialState);
return <Provider value={{ state, dispatch }}>{children}</Provider>;
};
export { store, StateProvider }
socket-instance.js
mport React, { useEffect, useContext, useState } from 'react';
import { store } from '../shared/store';
import socketio from 'socket.io-client';
const ENDPOINT = "http://localhost:5000/";
function SocketInstance() {
const { dispatch } = useContext(store);
const [socketInstance, setSocket] = useState(null);
useEffect(() => {
const currentSocket = socketio.connect(ENDPOINT, {
reconnection: true
})
currentSocket.on('connect', function (msg) {
console.log("Server connected...");
});
currentSocket.on("disconnect", function () {
console.log("server disconnected from client");
});
setSocket(currentSocket)
console.log("=======set socket instance==========");
console.log(socketInstance);
dispatch({ type: 'socket', socket: socketInstance })
}, [])
return (
<div >
</div>
)
}
export default SocketInstance;
device.js
function DeviceStatus() {
const { state, dispatch } = useContext(store);
const { deviceStatus, websocket } = state; //deviceCommand should be same as setting new state in store
const [isEnable, setIsEnabled] = useState(false)
const toggleSwitch = (checked) => {
setIsEnabled(checked)
dispatch({
type: "updateDeviceStatus",
deviceId: 21,
deviceState: checked,
})
console.log("******before emit***************");
console.log(deviceStatus);
console.log("******check websocket ***************");
console.log(websocket);
if (websocket) {
console.log("websocket instance can accessible");
// websocket.emit("datafromui", deviceStatusFromUI)
}
}
return (
<div className="devicestatus-item" >
<span>{DeviceStatusConst.FRONT_LL}</span>
<Switch checked={isEnable} onChange={toggleSwitch} />
</div>
)
}
export default DeviceStatus
Help will very much appreciated, "Thanks in advance"