react-web-chat React component
npm install react-web-chat[![Travis][build-badge]][build]
[![npm package][npm-badge]][npm]
[![Coveralls][coveralls-badge]][coveralls]
react-web-chat is an instant messaging UI built with React.
* Table of contents
* Installation
* Usage
* As a stand-alone module
* As a react component
* Custom events
* Listening
* Dispatching
* Custom themes
* Custom network clients
Simply install it with your favorite package manager:
npm install --save-dev react-web-chat yarn add react-web-chat
The stand-alone version of the module will render to a supplied dom element.
It accepts the following parameters:
| Argument | Description | Required | Type |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- | ------- |
| url | The url of your chat server | yes | String |
| element | The element react-web-chat should render to | yes | Element |
| theme | A custom theme | no | Object |
| client | A custom client | no | Object |
| typingStatus | Configuration options for the typing status indicator. Note! This delay will be a compounded value as per all the settings you provide. | no | Object |
| network | Configuration options for network communication of the default Feersum Client (NOTE! Required if using the default client, channel_id field must also be specified else the Feersum Client connection will fail! See below example.) | no | Object |
Communication with the module is handled via custom events described here.
``js
new ReactWebChat({
url: 'http://localhost:8000',
element: myChatElement,
typingStatus: {
active: true || false, // Enable/disable typing status indicator (default = true)
baseDelay: 500, // How many ms to show the indicator for (default = 750)
variance: 250, // How many ms to vary the delay by (default = 250)
letterDelay: 30, // How many ms to add for each letter in a message (default = 20)
minDelay: 200, // The minimum delay allowed. (default = 200)
maxDelay: 3000 // The maximum delay allowed. (default = 3000)
},
network: {
channel_id: 'f8472758-f804-4a7e-a225-5e303e121099', // The required channel_id for the default feersum client.
address: 'a6424358-g73g-7h8d-92m8-6s890g5892n07', // An optional address to specify.
startNew: true, // Specify if the chat is a new instance.
retransmissionTimeout: 500, // How many ms to wait between network request retries compounded by the amount of attempts already past.
retransmissionTimeoutMax: 1000, // The maximum compounded wait in ms between network connection requests.
retransmissionAttempts: 10, // Retry limit
eventNamespace: "rwc" // Custom even namespace
}
}
});
`
`js
import ReactWebChat from 'react-web-chat';
let myChatElement = document.getElementByID('my-chat-element');
const reactWebChat = new ReactWebChat({
url: 'http://localhost:8000',
element: myChatElement
});
`
`js
var ReactWebChat = require('react-web-chat').default;
var myChatElement = document.getElementByID('my-chat-element');
var reactWebChat = new ReactWebChat({
url: 'http://localhost:8000',
element: myChatElement
});
`
react-web-chat is also available as a UMD module. Simply load the module and instantiate a new instance as described in the example below. react
NOTE: and react-dom are peer dependencies so make sure they are loaded too
` The exported | Argument | Description | Required | Type | const MyComponent = props => ( Communication with the Custom Any dispatched redux action will fire a custom event using the following type: The A full list of actions to listen for: * rwc-MESSAGE_ADD > Note: event namespaces can be configured by passing in the following configuration to the constructor: Dispatching an event follows the same namespaced convention as described above. var message = { sendRWCMessage(message); The following components can be overridden: * AvatarComponent Please consult the API documentation as a guide to help you develop custom components for your themes. // your custom theme components const MyComponent = props => ( Network clients have the following responsibilities: * determine which protocol/standard to use (WS, socket.io, HTTP, XHR, Fetch, etc.) It's also the default client used by * Feersum Engine In future there will hopefully be several clients to support a wider range of IM back-ends. Writing a custom network client is easy :) All you need is an object with the following methods: send(message) { onmessage(fn) { onclose(fn) { [build-badge]: https://img.shields.io/travis/user/repo/master.png?style=flat-squarehtml
>
`ReactWebChatComponentAs a react component
can be used within an existing react application. `
It accepts the following parameters:
| -------- | ---------------------------------- | -------- | ------ |
| url | The url of your chat server | yes | String |
| theme | A custom theme | no | Object |
| client | A custom client | no | Object |$3
jsx`
import { ReactWebChatComponent } from 'react-web-chat';
);react-web-chatCustom events
module is handled via a series of custom events.react-web-chatListening
events are namespaced using the rwc- prefix.rwc-ACTION_TYPE`$3
js`
window.addEventListener('rwc-MESSAGE_RECEIVE', function(data) {
// do something with data
});data parameter object adheres to the CustomEvent specification:`
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
* rwc-MESSAGE_SEND
* rwc-MESSAGE_RECEIVE
* rwc-MESSAGE_QUEUE
* rwc-CONNECTION_ESTABLISHED
* rwc-CONNECTION_ATTEMPTED
* rwc-CONNECTION_DROPPED
* rwc-CONNECTION_LISTENING
>
> js`
> new ReactWebChat({
> / ... /
> network: {
> eventNamespace: "your-custom-namespace"
> }
> }
> your-custom-namespace-ACTION_TYPE
>
> This will result in the following event type:
> MESSAGE_SENDDispatching
However not all redux actions can be dispatched via custom events.
Currently only the action type is supported.`
More action types may be supported in future releases if justifiable use cases can be demonstrated.$3
js`
function sendRWCMessage() {
var rwcEvent = window.CustomEvent('rwc-MESSAGE_SEND', {
detail: {
payload: message
}
});
window.dispatchEvent(rwcEvent);
}
type: 'message',
layout: 'plain',
pages: [
{
text: 'Hello world'
}
]
};react-web-chatCustom themes
allows you to inject custom react components for specific parts of the UI.`
Any components not specified in the custom theme object will use the default theme's components.
* ButtonComponent
* CheckboxMenuComponent
* ImageComponent
* InputComponent
* MenuComponent
* TextComponent
* TypingIndicatorComponent$3
jsx`
import { ReactWebChatComponent } from 'react-web-chat';
import React from 'react';
import Avatar from './customTheme/Avatar';
import Button from './customTheme/Button';
import Input from './customTheme/Input';
theme={{
AvatarComponent: Avatar,
ButtonComponent: Button,
InputComponent: Input
}}
/>
);react-web-chatCustom network clients
supports custom network clients to manage network communication with your server.rwc-feersum-client
* translate messages to a format the server understands.react-web-chat which happens to make use the feersum message schema.`
Further reading:
* Feersum 0.9 SchemaWriting your own network client
js``
const feersumClient = {
init(url) {
// Connect to server, then bind "onmessage" and "onclose" methods.
},
// Translate message from feersum schema, then send to server.
},
// Translate message to feersum schema, then execute callback function with message as parameter.
},
// Execute callback when the connection is closed.
}
};
[build]: https://travis-ci.org/user/repo
[npm-badge]: https://img.shields.io/npm/v/npm-package.png?style=flat-square
[npm]: https://www.npmjs.org/package/npm-package
[coveralls-badge]: https://img.shields.io/coveralls/praekelt/react-web-chat/master.png?style=flat-square
[coveralls]: https://coveralls.io/github/praekelt/react-web-chat