Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
[![Build Status](https://travis-ci.org/mfrachet/use-socketio.svg?branch=master)](https://travis-ci.org/mfrachet/use-socketio)
[![Coverage Status](https://coveralls.io/repos/github/mfrachet/use-socketio/badge.svg?branch=master)](https://coveralls.io/github/mfrachet/use-socketio?branch=master)

Use [socket.io](https://socket.io/) using React Hooks (React v16.7+)
Use [socket.io](https://socket.io/) using React Hooks (React v16.8+)

# Usage

## Installation

```
$ yarn add use-socketio
```

# In your code
## In your code

```javascript
import { ClientSocket, useSocket } from "use-socketio";

const Hello = () => {
/**
* The first element is event data
* The second element is the socket(io) himself
* The key in useSocket is the event name to listen to
**/
const [lutin, socket] = useSocket("lutin");

useEffect(() => socket.emit("lutin", "hahah"), []);

return <div>Hello {lutin}</div>;
const Twitter = () => {
const [tweets, setTweet] = useState([]);

const socket = 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 = () => (
<ClientSocket url="http://localhost:3000/">
<Hello />
<ClientSocket url="https://some-tweet-socket-listener/">
<Twitter />
</ClientSocket>
);
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-socketio",
"version": "0.0.1",
"version": "1.0.0",
"description": "React hooks to use with https://socket.io/",
"main": "./lib/index.js",
"author": "Marvin Frachet <marvin.frachet@gmail.com>",
Expand Down