true-smart-messaging
v1.3.0
Published
``` npm install true-smart-messaging or yarn add true-smart-messaging ```
Downloads
7
Readme
Installation
npm install true-smart-messaging
or
yarn add true-smart-messaging
Create Socket
import trueSmartMessaging from 'true-smart-messaging';
const socket = trueSmartMessaging.connect('http://host.com');
List of methods
- Socket State
- Socket Disconnect
- Connection State
- Create Room
- Join Room
- Leave Room
- Broadcast Room
- Events
- Example Usage
- Socket State Helper
Socket State
Listening to State from Socket server has change.
socket.socketState(state => {
// Do Something
});
Socket Disconnect
Disconnects the socket manually.
socket.disconnect();
Connection State
socket.connection.connect(() => console.log('Connected'));
socket.connection.disconnect(() => console.log('Disconnect'));
socket.connection.connectError(() => console.log('Connect Error'));
socket.connection.reconnecting(() => console.log('Reconnecting'));
Create Room
For Setting page that read a config from the registry.
const configs = {
shop: {
id: '001',
name: 'Shop 1'
},
counter: {
id: '1',
name: 'Counter 1'
}
};
socket.createRoom(configs);
Join Room
Room's format is {shop.id}-{counter.id}
socket.joinRoom('room', 'clientName', response => {
// response will be { status, message }
});
Leave Room
socket.leaveRoom();
Broadcast Room
Listen
Listening to broadcast from the room.
socket.broadcastRoom.listen(data => {
// Do Something
});
Send
Send broadcast to the room.
socket.broadcastRoom.send(room, data);
Destroy
Remove listener
socket.broadcastRoom.destroy();
Events
| Event | KEY |
| ----------- | ------------ |
| ID Card | idCard
|
| E Signature | eSignature
|
| Image | image
|
| Info | info
|
Use event with KEY
socket.KEY.waitingForRequest;
socket.KEY.request;
socket.KEY.send;
socket.KEY.listen;
socket.KEY.destroy;
How to use
Waiting for request
Listening to another clients call request
socket.KEY.waitingForRequest(response => {
// response will be { clientId, data }
});
Request
Call request
for trigger waitingForRequest
.
You can send data to target.
socket.KEY.request(data);
Send
Send Data to Client that requested.
const data = {
status: 'SUCCESS', // BUSY, ERROR, CANCEL or etc.
data: '' // any
};
socket.KEY.send(clientId, data);
Listen
Listening to data when client sent.
socket.KEY.listen(data => {
// Do Something
});
Destroy
Remove all listeners when go out of the page or component will unmount.
socket.KEY.destroy();
Example Usage
Sender
import trueSmartMessaging from 'true-smart-messaging';
const socket = trueSmartMessaging.connect('http://host.com');
// Room's format is {shop.id}-{counter.id}
const room = '001-1';
const clientName = 'Agent';
socket.joinRoom(room, clientName);
socket.broadcastRoom.listen(() => alert('Broadcast'));
socket.idCard.waitingForRequest(({ clientId, data }) => {
socket.idCard.send(clientId, {
status: 'SUCCESS', // BUSY, ERROR
data: {} // ID Card data
});
});
// Destroy when go out of the page or component will unmount
socket.idCard.destroy();
Receiver
import trueSmartMessaging from 'true-smart-messaging';
const socket = trueSmartMessaging.connect('http://host.com');
// Room's format is {shop.id}-{counter.id}
const room = '001-1';
const clientName = 'CLIENT-NAME';
socket.joinRoom(room, clientName);
socket.broadcastRoom.listen(() => alert('Broadcast'));
socket.idCard.listen(respone => {
if (respone.status === 'SUCCESS') {
doSomething(respone.data);
}
});
socket.idCard.request();
// Destroy when go out of the page or component will unmount
socket.idCard.destroy();
Socket State Helper
| Name | Param required (Object) |
| ----------------------- | ----------------------------- |
| getConnectionInRoom()
| { state, room }
|
| isClientInRoom()
| { state, room, clientName }
|
Example
import trueSmartMessagingHelper from 'true-smart-messaging/helper';
socket.socketState((state, options) => {
const room = '001-1';
const clientName = 'CLIENT-NAME';
const connections = trueSmartMessagingHelper.getConnectionInRoom({ state, room });
// Do Something
});