robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.62k stars 135 forks source link

Send/Emit to socketIO with useSocketIO #173

Closed siddharthkul closed 1 year ago

siddharthkul commented 1 year ago

Hello, I couldn't find documentation for this but how do we send a message to a certain topic with useSocketIO hook ?

Below is the vanilla socketIO implementation that I'm trying to achieve

socket.emit("chat_room", { name: "John", message: "Hello World" });
siddharthkul commented 1 year ago

Leaving what worked for me here:

const payload = {
  name: "John",
  message: "Hello World"
};
sendMessage(`42["chat_room",${JSON.stringify(payload)}]`);

Seems a little hackey :(

siddharthkul commented 1 year ago

Link to socketio encoding/decoding protocol - https://github.com/socketio/socket.io-protocol

siddharthkul commented 1 year ago

I ended up using implementation as shown in this article - https://socket.io/how-to/use-with-react-hooks