Skip to content

SamuelWitke/SimpleChat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleChat

Simple React Node Websocket chat server and client

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

This appliction integrates with redux by passing a thunk to the onmessage recieve function.

 	const webSocket = Singleton.getInstance();
	const receive = (onMessageCb) => {
		webSocket.onmessage = (event) => onMessageCb(JSON.parse(event.data));
	};

	const send = (type, payload) =>
	webSocket.send(JSON.stringify({ type, payload }));
	webSocket.onopen = () => resolve({ send, receive });
	webSocket.onclose = () => { 
		webSocket.close();
		reject(new Error("WebSocket Closed"));
	}          
	...
  	const middleware=[thunkMiddleware.withExtraArgument({ send }), logger];
  	const store = createStore(
    		rootReducer,
    		initialState,
    		applyMiddleware(...middleware),
  	);
  	receive(store.dispatch);