Redux-thunk with websites

I want to create an on-demand website when some components want to subscribe to data. How can I share an instance of websocket using the shrink method?

action.js

export function subscribeToWS(url) {
   return dispatch => {
      let websocket = new WebSocket(url)
      websocket.on('connect', () => {
         websocket.send("subscribe") 
      }
      websocket.on('message', (message) => {
        dispatch(storeNewData(message))
      }
   } 
}

I could do something like this, but that would require a new instance for each new subscription.

+4
source share
3 answers

redux-websocket-bridge. Web Socket Redux Redux Web Socket.

: Redux API, REST API .

, Flux Standard Action, redux-websocket-bridge . string, ArrayBuffer Blob. , , Flux, . Slack RTM API.

0

, . @matthewatabet @abguy, https://github.com/luskhq/redux-ws , , Redux Thunk, - .

, , Github, . socket.io, - .

, dispatch addNewItemSocket:

<RaisedButton
    label="Click to add!" primary={true}
    onTouchTap={ () => {
        const newItem = ReactDOM.findDOMNode(this.refs.newTodo.input).value
        newItem === "" ?  alert("Item shouldn't be blank")
                       :  dispatch(addNewItemSocket(socket,items.size,newItem)) 
                        {/*: dispatch(addNewItem(items.size,newItem))*/}
        ReactDOM.findDOMNode(this.refs.newTodo.input).value = ""
      }
    }
/>

addNewItemSocket :

export const addNewItemSocket = (socket,id,item) => {
    return (dispatch) => {
        let postData = {
                id:id+1,
                item:item,
                completed:false
             }
        socket.emit('addItem',postData)     
    }   
}

, :

socket.on('itemAdded',(res)=>{
   console.dir(res)
   dispatch(AddItem(res))
})

actoins AddItem :

export const AddItem = (data) => ({
    type: "ADD_ITEM",
    item: data.item,
    itemId:data.id,
    completed:data.completed
})

This is still new to me, so any feedback is welcome. I will also write a PR from https://github.com/luskhq/redux-ws to give an example given there.

0
source

All Articles