Coder Social home page Coder Social logo

cliffhall / react-chat-client Goto Github PK

View Code? Open in Web Editor NEW
25.0 5.0 12.0 420 KB

A simple chat client built in React for communicating with the node-multi-server-chat example

License: MIT License

JavaScript 99.58% HTML 0.42%
react beginner-friendly chat-application chat socket-io socket-io-client javascript es6 git-submodule

react-chat-client's Introduction

react-chat-client

What is this?

A React-based chat client for communicating with my node-multi-server-chat example.

This client differs from the minimalist one that comes with the server project in a few ways:

  • It lets you log in with any number of different users (instead of the predefined Anna and Billy)
  • It allows you to choose the recipient of each outgoing message from a dropdown of all connected users
  • It displays scrollable message history
  • It notifies you when your selected recipient disconnects
  • If your lost recipient reconnects, it notifies you and selects them again in the dropdown
  • It stores and displays messages by thread, so you can carry on separate conversations with different users
  • When you receive a message from another user, it automatically selects them as a recipient and shows their thread

You can read a tutorial about how it was built with only React and then refactored to use Redux.

Two users chatting

Two users chatting

Setup

Install Node and npm

Node 7.7 or above (also installs npm)

Install Node modules

cd path/to/react-chat-client (the rest of this document assumes you are at this location)

npm install

Initialize server project

For convenience, this project pulls in the node-multi-server-chat project as a Git submodule. After you run the following command, you'll find that project in the server folder, with its node-modules installed.

npm run install-server-module

Launch

Launching Socket Server Instances

Several npm scripts have been defined in package.json for launching the socket server instances.

Inside your IDE

If you're running a modern IDE like Webstorm, you can just open the npm window and double-click on each start-server-x script. A separate integrated terminal will launch and you can monitor each instance's log output.

From the command line

In each of four separate terminal windows, enter one of the following commands:

npm run start-server-1

npm run start-server-2

npm run start-server-3

npm run start-server-4

Launching the Server for the React Chat Client

The react-scripts library will compile the client bundle and start the server for the client.

npm run serve-client

Once that's done, open a couple of browser windows, navigate to http://localhost:3000/, enter two different user names, choose a server port, and connect. It doesn't matter if you're on different ports or the same port, the servers will make sure your messages make it to each other.

Note that when you connect the first user, you won't see anything other than a status message of 'Connected' and the 'Connect' button will change to 'Disconnect'. As soon as you sign in another user, you'll see a dropdown with 'Choose someone to message' in it. As users connect and disconnect, this list will be updated on all connected clients, and of course your own name won't be listed.

Implementation

This React client uses:

  • redux to manage application state
  • react-redux to inject the store's dispatch function and selected parts of the application state into any component's props.

Protocol handling

The protocol is outlined in the chat server README, so there's no need to duplicate that here. This client operates the same as the minimalist chat client in the server project, except all the protocol handling is encapsulated in the Socket class.

The socketMiddleware middleware instantiates a Socket instance, passing in callbacks for

  • onConnectionChange, called when the socket connection state changes
  • onError, called when the socket experiences an error condition, so status can be reported
  • onIncomingMessage, called when an instant message is received
  • onUpdateClient, called when the server updates the client with the list of connected users

TODO

react-chat-client's People

Contributors

cliffhall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-chat-client's Issues

Reducers are definitely not pure

Redux reducers are supposed to be "pure functions". That means they are only (state, action) => newState, with no side effects - no async calls, no sockets, no accessing the rest of the store directly, no global variables, or anything else.

These lines are definitely not supposed to be in a reducer:

https://github.com/cliffhall/react-chat-client/blob/master/src/store/message/reducer/index.js#L87-L93

They should go into a middleware instead. See the Redux FAQ entry on where websockets should live for examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.