bs-react-native-websocket
v1.0.1
Published
Buckle Script bindings for react-native-websocket
Downloads
1
Readme
bs-react-native-websocket
This package provide Reasonml bindings for react-native-websocket component.
Installation
$ yarn add bs-react-native-websocket react-native-websocket
Then update your bsconfig.json
, add bs-react-native-websocket
to bs-dependencies.
"bs-dependencies": ["bs-react-native-websocket"]
Usage
Usage is very similliar to react-native-websocket.
Example:
type state = {
socketRef: ref(option(ReasonReact.reactRef)),
};
let setSocketRef = (theRef, {ReasonReact.state}) =>
state.socketRef := Js.Nullable.toOption(theRef);
let component = ReasonReact.reducerComponent("Component");
let make =
(
_children,
) => {
...component,
initialState: () => {socketRef: ref(None)},
render: self =>
<View>
<ReactNativeWebsocket
url="ws://localhost:5000"
onOpen={() => Js.log("successfull connection")}
onMessage={
event =>
Js.log(event) // event is a Js.dict object
}
onError={() => Js.log("get error")}
onClose={() => Js.log("connection closed")}
ref={self.handle(setSocketRef)}
reconnect=true
/>
</View>,
};
Example of sending data:
// socket is ref to ReactNativeWebsocket component
// data is a stringifiedData that you want to send
ReactNativeWebsocket.send(socketRef, data);
License
MIT
Contributing
PR's are welcome!