Requests & Websocket
Requests
Location: client/base/api/requests.js
API Requests is one of the features coming from Harmony.
Requests class include the following methods:
call
Call to API
Parameters:
config - axios config json.
Usage
import from src/base/base-api/index.ts
request.call({
method: 'post',
baseURL: baseURL,
url: 'users/login',
data: data
});
broadcastAction
Invoke action via websocket to every online client.
Parameters:
action - Action that you want to execute.
Usage
import from client/base/api/requests.js
import {UserTypes} from '../redux/user';
requests.broadcastAction({type: UserTypes.FETCH_POSTS, payload: null});
Secured Actions
Harmony know about the risk to dispatch actions via client to aonther clients.
Therefore, if you want to disptach action with websocket actions, you need to allow the action in server.
Read more about websocket in server docomentation.
Start Websocket Action
in client/indexjs
you can find the starter to websocket action.
const wsAction = new WSAction(store, config.ROOT_WS_URL, {
retryCount:3,
reconnectInterval: 3
});
wsAction.start();
Requests Definitions File
Location: client/requests/index.js
In requests file we define all the requests calls and use it in sagas. Harmony prefer to use one file to export requests definitions for Best Practice.
Example Code
createUser: (data) => {
return request.call({
method: 'post',
baseURL: baseURL,
url: '/users',
data: data
});
}