Skip to content

nerdchanii/socket-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

it is websocket-store for using easily websocket

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published