Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

use-socket.io-client

1.1.0 • Public • Published

use-socket.io-client

Build Status

React(^16.8.0) hook for socket.io-client, manipulate socket.io client without any side effect.

//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

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..');

Install

npm i use-socket.io-client

DownloadsWeekly Downloads

352

Version

1.1.0

License

MIT

Unpacked Size

3.71 kB

Total Files

6

Last publish

Collaborators

  • avatar