Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iamgyz/use-socket.io-client
https://www.npmjs.com/package/use-socket.io-client
https://github.com/iamgyz/use-socket.io-client
react react-hooks reactjs
Last synced: 11 days ago
JSON representation
https://www.npmjs.com/package/use-socket.io-client
- Host: GitHub
- URL: https://github.com/iamgyz/use-socket.io-client
- Owner: iamgyz
- Created: 2019-02-08T11:39:36.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-03T09:40:24.000Z (over 4 years ago)
- Last Synced: 2024-11-24T21:04:59.403Z (19 days ago)
- Topics: react, react-hooks, reactjs
- Language: JavaScript
- Size: 5.86 KB
- Stars: 50
- Watchers: 2
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- fucking-awesome-react-hooks - `use-socket.io-client` - client, manipulate 🌎 [socket.io](socket.io) client without any side effect. (Packages)
- awesome-react-hooks-cn - `use-socket.io-client` - client, manipulate [socket.io](https://socket.io) client without any side effect. (Packages)
- awesome-react-hooks - `use-socket.io-client` - client, manipulate [socket.io](https://socket.io) client without any side effect. (Packages)
- awesome-react-hooks - `use-socket.io-client` - client, manipulate [socket.io](https://socket.io) client without any side effect. (Packages)
README
# use-socket.io-client
[![Build Status](https://travis-ci.org/iamgyz/use-socket.io-client.svg?branch=master)](https://travis-ci.org/iamgyz/use-socket.io-client)React(^16.8.0) hook for socket.io-client, manipulate socket.io client without any side effect.
```javascript
//In legacy, you would write like this:
import io from 'socket.io-client';
socket = io('ws://host:port');//Now, you could easily write this inside the body of a function component:
import useSocket from 'use-socket.io-client';
const [socket] = useSocket('ws://host:port')
```## Installation
```
$ npm i use-socket.io-client
```## Example
```javascript
import useSocket from 'use-socket.io-client';//You can treat "useSocket" as "io"
const [socket] = useSocket('ws://localhost:8080',{
autoConnect: false,
//any other options
});
//connect socket
socket.connect();//add event
socket.on('message',(text)=>{
console.log(text);
});//emit
socket.emit('message','this is demo..');
```