# @tomas2387/use-socketio

> React hooks for https://socket.io/

Latest version **3.0.1** (published 2023-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tomas2387/use-socketio
pnpm add @tomas2387/use-socketio
yarn add @tomas2387/use-socketio
bun add @tomas2387/use-socketio
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2023-11-14 |
| First published | 2023-11-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Marvin Frachet |
| Maintainers | tomas2387 |
| Keywords | react, context |

## Links

- npm: https://www.npmjs.com/package/@tomas2387/use-socketio
- Repository: https://github.com/tomas2387/server-push-hooks
- Issues: https://github.com/tomas2387/server-push-hooks/issues
- npm.io page: https://npm.io/package/@tomas2387/use-socketio

## Dependencies (1)

- [socket.io-client](https://npm.io/package/socket.io-client.md) ^4.7.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2023-11-14
- 3.0.0 — 2023-11-14
- 2.1.0 — 2023-11-14

## README

Use [socket.io v4](https://socket.io/) with React, in hooks (React v16.8+).

# Usage

## Installation

```sh
$ yarn add @tomas2387/use-socketio
```

## In your code

### useSocket hook

Listen to a specific event and trigger the according callback every time there's one. **This hooks doesn't trigger a re-render. You have to manage it yourself.**

```jsx
import { SocketIOProvider, useSocket } from "use-socketio";

const Twitter = () => {
  const [tweets, setTweet] = useState([]);

  const { socket, subscribe, unsubscribe } = useSocket("tweet", (newTweet) =>
    setTweet([newTweet, ...tweets])
  );

  return tweets.length ? (
    <ul>
      {tweets.map((tweet) => (
        <li key={tweet.id}>{tweet.text}</li>
      ))}
    </ul>
  ) : (
    <p>Actually waiting for the websocket server...</p>
  );
};

const App = () => (
  <SocketIOProvider url="http://localhost:3000" opts={socketIoOptions}>
    <Twitter />
  </SocketIOProvider>
);
```

_The socketio options to pass to the provider are available here: https://socket.io/docs/client-api/#new-Manager-url-options._

### useLastMessage hook

Listen to the latest message received on a specific event name. **This hook triggers a re-render so you don't have to.**

```jsx
import { SocketIOProvider, useLastMessage } from "use-socketio";

const Twitter = () => {
  const { data: lastMessage, socket, subscribe, unsubscribe } = useLastMessage(
    "tweet"
  );

  return <p>{lastMessage || "Waiting for some tweets"}</p>;
};

const App = () => (
  <SocketIOProvider url="http://localhost:3000">
    <Twitter />
  </SocketIOProvider>
);
```

## Notes

For example on how to implement a Socket.io server, you can take a look at the [socket.io the example folder](../../example/socketio/socketio-server.js).

---
_Source: https://npm.io/package/@tomas2387/use-socketio · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
