-
Notifications
You must be signed in to change notification settings - Fork 0
Docs: rewrite the public home and getting-started flow #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+92
−12
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,97 @@ | ||
| # react-socket-store | ||
|
|
||
| `react-socket-store` is the React adapter for the public `socket-store` | ||
| package. It provides a provider and hooks for reading topic state, sending | ||
| topic payloads, and cleaning up subscriptions with React components. | ||
| `react-socket-store` connects `socket-store` to React with a provider and hooks | ||
| for reading topic state, sending payloads, and cleaning up subscriptions in | ||
| components. | ||
|
|
||
| Use these docs for user-facing React integration guidance. | ||
| Use it when you already have a `SocketStore` instance and want a React-friendly | ||
| way to share it through context or pass it directly into a focused client | ||
| subtree. | ||
|
|
||
| ## Sections | ||
| ## Install | ||
|
|
||
| - [Guide](./guide/): provider setup and hook usage. | ||
| - [API](./api/): exported React adapter surface. | ||
| - [Examples](./examples/): runnable or directly adaptable usage notes. | ||
| - [Compatibility](./compatibility/): package ownership, version pairing, and | ||
| release order. | ||
| - [Migration](./migration/): current package-boundary guidance. | ||
| - [Next.js](./nextjs/): current client-boundary status. | ||
| ```sh | ||
| npm install react-socket-store socket-store | ||
| ``` | ||
|
|
||
| ## Getting Started | ||
|
|
||
| Create the `SocketStore` first, then choose either `SocketProvider` or the | ||
| store-direct hooks: | ||
|
|
||
| ```tsx | ||
| import { useEffect, useState } from "react"; | ||
| import { | ||
| SocketProvider, | ||
| SocketStore, | ||
| createMessageHandler, | ||
| useSocket, | ||
| type ISocketStore, | ||
| } from "react-socket-store"; | ||
|
|
||
| type ChatSchema = { | ||
| talk: { | ||
| state: string[]; | ||
| payload: string; | ||
| }; | ||
| }; | ||
|
|
||
| function ChatMessages() { | ||
| const [messages, sendTalk] = useSocket<ChatSchema, "talk">("talk"); | ||
|
|
||
| return ( | ||
| <button type="button" onClick={() => sendTalk("hello")}> | ||
| Messages: {messages.length} | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| export function ChatBoundary() { | ||
| const [store, setStore] = useState<ISocketStore<ChatSchema> | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const socket = new WebSocket("wss://example.com/chat"); | ||
| const nextStore = new SocketStore(socket, [ | ||
| createMessageHandler<string[], string>( | ||
| "talk", | ||
| (messages, message) => [...messages, message], | ||
| [] | ||
| ), | ||
| ]) as unknown as ISocketStore<ChatSchema>; | ||
|
|
||
| setStore(nextStore); | ||
|
|
||
| return () => { | ||
| socket.close(); | ||
| }; | ||
| }, []); | ||
|
|
||
| if (store === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <SocketProvider store={store}> | ||
| <ChatMessages /> | ||
| </SocketProvider> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Choose An Integration Shape | ||
|
|
||
| - [Guide](./guide/): provider setup, store-direct hooks, and cleanup guidance. | ||
| - [Examples](./examples/): provider, store-direct, and Next.js usage patterns. | ||
| - [API](./api/): exported hooks, provider types, and adapter surface. | ||
| - [Next.js](./nextjs/): client-boundary guidance for App Router projects. | ||
| - [Compatibility](./compatibility/): version pairing and package ownership. | ||
| - [Migration](./migration/): current migration notes and boundary changes. | ||
|
|
||
| ## What This Package Owns | ||
|
|
||
| `react-socket-store` owns the React adapter layer: `SocketProvider`, | ||
| store-direct hooks, `useSocketStoreRef`, schema-safe hook types, and React | ||
| subscription cleanup. | ||
|
|
||
| The framework-agnostic WebSocket behavior still belongs to | ||
| [`socket-store`](https://nerdchanii.github.io/socket-store/). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this getting-started snippet is copied against a real endpoint,
ChatMessagesrenders as soon as theSocketStoreis constructed, so the button can be clicked while theWebSocketis stillCONNECTINGor after the placeholder endpoint fails.useSocketforwards the click directly tostore.send, and the coresocket-storesend contract throwsERR_SOCKET_NOT_OPENunlessreadyState === 1, so the first click can crash the example; gate the button/rendering on an open/onConnect state or otherwise handle the not-open case.Useful? React with 👍 / 👎.