I am trying to create a chat, here is I have created instance of socket and unmounted it, but the issue is the connection shows false and true at the same time, The chats and messages are listened but cannot get the new messages.What is the correct way of using socket.io-client ? help!!
when I console.log(socket) on the client, this prints as follows :
Socket {connected: false, receiveBuffer: Array(0), sendBuffer: Array(0), ids: 0, acks: {…}, …}
acks: {0: ƒ}
connected: true
flags: {}
id: "_klZ4PeyF0r37wPcAABa"
ids: 1
io: Manager {nsps: {…}, subs: Array(5), opts: {…}, setTimeoutFn: ƒ, clearTimeoutFn: ƒ, …}
nsp: "/"
receiveBuffer: []
sendBuffer: []
subs: (4) [ƒ, ƒ, ƒ, ƒ]
_callbacks: {$chats: Array(1), $new_message: Array(1), $message_list: Array(1)}
active: (...)
disconnected: (...)
volatile: (...)
[[Prototype]]: Emitter
when I consolelog(socket.connected),it also returns false, I have created the instance like this,
useEffect(() => {
socket = io(`https://samsara-chat.herokuapp.com/?token=${token}`, {
transports: ['websocket']
// autoConnect: true
})
socket?.connect()
socket?.emit('chat-list', {})
socket?.on('chats', (args) => {
setChats(args.chats)
})
socket?.emit('list', { room_id: currentroom })
socket?.on('message_list', (args) => {
setMessage((list) => [...args.messages])
})
return () => {
socket?.disconnect()
}
}, [socket, currentroom])