socket-store
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Hits

Socket-Store

It is Websocket Store

How to use

1. Install

# npm 
npm install socket-store

# yarn

yarn add socket-store

2. Create MessageHandler and SocketStore

2-1. Create MessageHandler


First, create a message handler.
Define the topic, callback for the topic, and default status. This will be provided in the store.

  • createMessageHandler(key, callback, state)
    • key : it will be subject of message.
    • callback: it will works like reducer. it must return state!
    • state: it is defualt state.
const talkHandler = createMessageHandler<string[], string>(
  "talk",
  (state, data) => {
    return [...state, data];
  },
  []
);

2-2. Create SocketStore

Next, create a socket store. Store gets two parameters for web sockets and message handlers.

//handlers = you can uses like this.
const talkHandler = createMessageHandler('talk', (state, data)=>[...state, data], []);
const tradingHandler = createMessageHandler('trade', (state,data)=>data, null);

const socket = new WebSocket("ws://localhost:3030");
const store = new SocketStore(socket, [talkHandler, tradingHandler]);

2-3. How to send and How to get state

// key is messagehandlers key, data: any type
store.send({key: 'talk', data: {user:'nerd', message:'hello'});

// give to key of messageHandler, then receive state about key.
store.getState('talk'); 
// return state of 'talk' topic
// ex) [{user:'nerd',message:'hi'}, {user:'chanii',message:'Can you help me? '}, {user: 'nerd', message:'kk'}] 

LICENSE

MIT

Package Sidebar

Install

npm i socket-store

Weekly Downloads

6

Version

0.0.2

License

MIT

Unpacked Size

13.6 kB

Total Files

19

Last publish

Collaborators

  • nerdchanii