bs-react-native-websocket

1.0.1 • Public • Published

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!

Readme

Keywords

none

Package Sidebar

Install

npm i bs-react-native-websocket

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

26.6 kB

Total Files

19

Last publish

Collaborators

  • trensik