mkdm-react-websocket-hook
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

ready to use Websocket hook based on Socket.IO

Description

Basic example

import useWebSocket from "mkdm-react-websocket-hook";

export default function App(){
    const {emit, listen, emitAndListen} = useWebSocket({
      url: "https://yourserverurl",
      socketServerPath: "/yoursocketionamespace",
      stateUpdateCallback: (state) => {
          if(state.error){
            console.log(state.error)
          }else if(state.state === "disconnect"){
            console.log(state.reason)
          }else{
            console.log(state.state)
          }
      },
      corsOrigin: "*"
    })

    useEffect(() => {
        listen("pong",(msg) => console.log(msg))
    },[])

    return(
        <View>
          <Button title="Ping !" onPress={() => emit("ping","ping")} >
        </View>
    )
}

Licence

Package Sidebar

Install

npm i mkdm-react-websocket-hook

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

7.29 kB

Total Files

5

Last publish

Collaborators

  • mikadam