Skip to content

Instantly share code, notes, and snippets.

@tpjnorton
Last active October 1, 2020 12:06
Show Gist options
  • Save tpjnorton/52865bf734b3a9ee83c5d552eb8c59c7 to your computer and use it in GitHub Desktop.
Save tpjnorton/52865bf734b3a9ee83c5d552eb8c59c7 to your computer and use it in GitHub Desktop.
An example of using the useWebSocket hook.
const ChatView = () = {
const wsRef = useWebSocket(
'wss://example.com/chat',
null,
(message) => alert(message),
() => alert('error!')
);
return (
<div>
<button onClick={() => wsRef.current?.send('Hello, World!")}>Click Me!</button>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment