I'm using socket.io , but the connection keeps failing

I use in react
This is my socket instance

import { io } from 'socket.io-client'

const URL = 'wss://irc-ws.chat.twitch.tv:443'

const socket = io(URL, {
  transports: ['websocket'],
  autoConnect: false,
})

export default socket

This is my react code

useEffect(() => {
    const onConnect = () => {
      console.log('WebSocket Client Connected')
      socket.send(
        'CAP REQ :twitch.tv/membership twitch.tv/tags twitch.tv/commands'
      )
      socket.send(`PASS oauth:token`)
      socket.send(`NICK username`)
    }
    const onConnectError = (error: any) => {
      console.log(error)
    }
    const onMessage = () => {
      console.log('message')
    }

    socket.on('connect', onConnect)
    socket.on('connect_error', onConnectError)
    socket.on('message', onMessage)
    socket.connect()
    return () => {
      socket.off('connect', onConnect)
      socket.off('connect_error', onConnectError)
      socket.off('message', onMessage)
      socket.disconnect()
    }
  }, [status])

Failed to print WebSocket Client Connected message, only Error: timeout message

What should I do?

From https://socket.io/docs/v4/

You should not be attempting to use socket.io to connect to Twitch chat. Either use an actual twitch chat library like tmi.js, or use a websocket library.

1 Like

Thank you for your reply.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.