|
1 | | -React hooks for socket.io 4.x |
2 | | ---- |
3 | | -Usage: <br> |
4 | | -1. Wrap your components with the provider |
5 | | - |
6 | | -```tsx |
7 | | - import { IoProvider } from 'socket.io-react-hook'; |
8 | | - |
9 | | - <IoProvider> |
10 | | - <App /> |
11 | | - </IoProvider> |
12 | | -``` |
13 | | - |
14 | | -2. |
15 | | -```tsx |
16 | | - import { useSocket, useSocketEvent } from 'socket.io-react-hook'; |
17 | | - |
18 | | - const { socket, error } = useSocket(); |
19 | | - const { socket, lastMessage, sendMessage } = useSocketEvent(socket, 'message'); |
20 | | - |
21 | | -``` |
22 | | - |
23 | | -useSocket forwards all parameters to socket.io constructor.<br> |
24 | | -See the available options [here](https://socket.io/docs/v4/client-initialization/) |
25 | | - |
26 | | -If the socket connection depends on state, use it like this: <br> |
27 | | -The connection will be initiated once the socket is enabled.<br> |
28 | | -The connection for a namespace is shared between your components, feel free to use the hooks in multiple components. |
29 | | - |
30 | | -```tsx |
31 | | -import { useCookie } from 'react-use'; |
32 | | -import { useSocket } from 'socket.io-react-hook'; |
33 | | - |
34 | | -export const useAuthenticatedSocket = (namespace?: string) => { |
35 | | - const [accessToken] = useCookie('jwt'); |
36 | | - return useSocket(namespace, { |
37 | | - enabled: !!accessToken, |
38 | | - }); |
39 | | -}; |
40 | | - |
41 | | -``` |
42 | | - |
43 | | -The useSocket hook always returns a socket-like object, even before a succesful connection. You don't have to check whether it is undefined.<br> |
44 | | - |
45 | | -Example: |
46 | | - |
47 | | -```tsx |
48 | | -export const useAuthenticatedSocket = (namespace?: string) => { |
49 | | - const [accessToken] = useCookie('jwt'); |
50 | | - return useSocket(namespace, { |
51 | | - enabled: !!accessToken, |
52 | | - }); |
53 | | -}; |
54 | | -const Index = () => { |
55 | | - |
56 | | - const { socket, connected, error } = useAuthenticatedSocket(); |
57 | | - const { socket, lastMessage, sendMessage } = |
58 | | - useSocketEvent<string>(socket, 'eventName'); |
59 | | - |
60 | | - return <div>{ lastMessage }</div> |
61 | | -} |
62 | | -``` |
63 | | - |
64 | | -useSocketEvent will immediately return the last available value of lastMessage even on newly mounted components. |
65 | | - |
66 | | -Emitting messages works as always: |
67 | | - |
68 | | -```tsx |
69 | | - const { socket, connected, error } = useSocket(); |
70 | | - socket.emit('eventName', data); |
71 | | - |
72 | | -``` |
73 | | -Or by calling sendMessage |
74 | | -```tsx |
75 | | - const { socket, lastMessage, sendMessage } = useSocketEvent<string>(socket, 'eventName'); |
76 | | - sendMessage(data); |
77 | | - |
| 1 | +React hooks for socket.io 4.x |
| 2 | +--- |
| 3 | +Usage: <br> |
| 4 | +1. Wrap your components with the provider |
| 5 | + |
| 6 | +```tsx |
| 7 | + import { IoProvider } from 'socket.io-react-hook'; |
| 8 | + |
| 9 | + <IoProvider> |
| 10 | + <App /> |
| 11 | + </IoProvider> |
| 12 | +``` |
| 13 | + |
| 14 | +2. |
| 15 | +```tsx |
| 16 | + import { useSocket, useSocketEvent } from 'socket.io-react-hook'; |
| 17 | + |
| 18 | + const { socket, error } = useSocket(); |
| 19 | + const { lastMessage, sendMessage } = useSocketEvent(socket, 'message'); |
| 20 | + |
| 21 | +``` |
| 22 | + |
| 23 | +useSocket forwards all parameters to socket.io constructor.<br> |
| 24 | +See the available options [here](https://socket.io/docs/v4/client-initialization/) |
| 25 | + |
| 26 | +If the socket connection depends on state, use it like this: <br> |
| 27 | +The connection will be initiated once the socket is enabled.<br> |
| 28 | +The connection for a namespace is shared between your components, feel free to use the hooks in multiple components. |
| 29 | + |
| 30 | +```tsx |
| 31 | +import { useCookie } from 'react-use'; |
| 32 | +import { useSocket } from 'socket.io-react-hook'; |
| 33 | + |
| 34 | +export const useAuthenticatedSocket = (namespace?: string) => { |
| 35 | + const [accessToken] = useCookie('jwt'); |
| 36 | + return useSocket(namespace, { |
| 37 | + enabled: !!accessToken, |
| 38 | + }); |
| 39 | +}; |
| 40 | + |
| 41 | +``` |
| 42 | + |
| 43 | +The useSocket hook always returns a socket-like object, even before a succesful connection. You don't have to check whether it is undefined.<br> |
| 44 | + |
| 45 | +Example: |
| 46 | + |
| 47 | +```tsx |
| 48 | +export const useAuthenticatedSocket = (namespace?: string) => { |
| 49 | + const [accessToken] = useCookie('jwt'); |
| 50 | + return useSocket(namespace, { |
| 51 | + enabled: !!accessToken, |
| 52 | + }); |
| 53 | +}; |
| 54 | +const Index = () => { |
| 55 | + |
| 56 | + const { socket, connected, error } = useAuthenticatedSocket(); |
| 57 | + const { lastMessage, sendMessage } = |
| 58 | + useSocketEvent<string>(socket, 'eventName'); |
| 59 | + |
| 60 | + return <div>{ lastMessage }</div> |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +useSocketEvent will immediately return the last available value of lastMessage even on newly mounted components. |
| 65 | + |
| 66 | +Emitting messages works as always: |
| 67 | + |
| 68 | +```tsx |
| 69 | + const { socket, connected, error } = useSocket(); |
| 70 | + socket.emit('eventName', data); |
| 71 | + |
| 72 | +``` |
| 73 | +Or by calling sendMessage |
| 74 | +```tsx |
| 75 | + const { socket, lastMessage, sendMessage } = useSocketEvent<string>(socket, 'eventName'); |
| 76 | + sendMessage(data); |
| 77 | + |
78 | 78 | ``` |
0 commit comments