A react-redux inspired library to deal with websocket.
npm i --save @mesteche/react-socket
yarn add @mesteche/react-socket
Makes the socket available to the children, anywhere below in the tree.
An open <Socket>
is required for being able to use connect()
on sub-components.
children
the component down the tree. Must be an only child, if you need to have siblings, wrap them in a single component.id
if you want to have multiple socket connected at once, you must give them an id that will be used byconnect()
to identify the right socket.
If you want to instanciate the connection outside of the <Socket>
component, you should pass the WebSocket
instance as a prop:
socket
An instance of WebSocket
Else the <Socket>
component can create it with the following parameters as props:
url
The url to connect toprotocol
The protocol to useonopen
A function to call when the connection is establishedonerror
A function to call if an errors occursonmessage
A function to call when a message is recieved (the message will be passed as an argument)
// main.js
import React from 'react'
import {render} from 'react-dom'
import Socket from 'react-socket'
/* See connect() documentation below */
import Demo from './Demo.js'
render(
<Socket url='ws://localhost:8080'>
<Demo onWsMessage={msg => console.info('message recieved', msg)}>
Send Message
</Demo>
</Socket>,
document.querySelector('#app')
)
Connects a React component to a WebSocket
.
Similarly to react-redux's connect()
, it doesn't modify the component passed to it but returns a new component connected to the WebSocket
instead.
In addition to connect the component's props to the socket's send()
method, the new component will expose a onWsMessage
and socketId
prop, for you to register a callback when a message is recieved from the server and to select the socket you want to connect to.
For instance, if you already use react-redux, you can use use react-redux's connect()
on this new component to dispatch an action when a message arrives.
mapSendToProps
(Object or Function):- If an object is passed, each key is assumed to be a prop of the component, and the correspondign values are assumed to be functions that returns the data to be sent to the server. Each function will be wrapped into a
send()
call to the server. - If a function is passed, it will be given the
send()
function of the socket. It is expected that it returns an object containning the props. It's up to you to usesend()
in these props.
- If an object is passed, each key is assumed to be a prop of the component, and the correspondign values are assumed to be functions that returns the data to be sent to the server. Each function will be wrapped into a
Similar to connect()
, except instead of exposing onWsMessage
and socketId
as props on the returned component, it accepts a config
argument.
Same as connect()
plus :
[config]
(Object):[onWsMessage]
(Function): This function will be called when a message is recieved from the server, with the message as parameter.[socketId]
(String or Symbol): The id of the socket you're targetting.
In this example, 'Message' will be sent to the server when the button is clicked.
// Demo.js
import React from 'react'
import { connect } from 'react-socket'
// Basic React component
export const Demo = ({sendMessage, children}) => (
<button onClick={function () {sendMessage('Message')}}>
{children}
</button>
)
const mapSendToProps = {
sendMessage: msg => msg,
}
export default connect(mapSendToProps)(Demo)
This Example is similar to the previous one but mapSendToProps
is used as a function.
// Demo.js
import React from 'react'
import { connect } from 'react-socket'
// Basic React component
export const Demo = ({sendMessage, children}) => (
<button onClick={function () {sendMessage('Message')}}>
{children}
</button>
)
const mapSendToProps = send => ({
sendMessage: msg => send(msg),
})
export default connect(mapSendToProps)(Demo)
In this example, we use the onWsMessage
prop of the connected component to listen to the server.
We also use react-redux to respond to such event by dispatching an action (see redux and react-redux documentation).
// Demo.js
import React from 'react'
import { connect as wsConnect } from 'react-socket'
import { connect as RRConnect } from 'react-redux'
// Basic React component
export const Demo = ({sendMessage, children}) => (
<button onClick={function () {sendMessage('Message')}}>
{children}
</button>
)
const mapSendToProps = {
sendMessage: msg => msg,
}
const mapDispatchToProps = {
onWsMessage: msg => ({
type: 'SERVER_MESSAGE',
payload: msg.data,
})
}
export default RRConnect(null, mapDispatchToProps)(
wsConnect(mapSendToProps)(Demo)
)