Hello I am trying to save in a state the response that my socket sends me through the back end
This is a method where I send messages to my socket
export default class Home extends Component {
constructor(){
super()
this.state = {
informationReceived: 'Nothing yet!'
}
const token = localStorage.getItem('token');
const socket = io('http://localhost:8080', {
query: { token: token }
});
socket.on('success', (receivedInfo) => {
this.setState({
informationReceived: receivedInfo
})
})
}
emitInfoToAll = () => {
const token = localStorage.getItem('token');
console.log('entrou')
const socket = io('http://localhost:8080',{
query: { token: token }
});
socket.emit('myevent', 'Hello realtime connected users!')
console.log(this.state.informationReceived);
}
}
But this way I would be opening connection to my socket twice when sending and receiving I was wondering how I can only open one connection so when I send a reply to my backend I don't have to open again
and how could I set these values that I get for my p's tags:
<p> Name: <span className = "name"> </span> </p>
<p> Points: <span className = "points"> 0 </span> </p>