Skip to content

Creating new adapters to connect to a different backend

Cato Gonzalez edited this page Feb 8, 2018 · 2 revisions

To have the chat widget talk to a different backend, you should create a node module that exposes the following API:

Method Returns Description
init(config) Promise This method is called upon chat-widget initialization and should perform any tasks required to setup the connection with your backend. It receives a config JS object with whatever configuration you need to setup communication with your backend.
send(data) Nothing Method to send any data to your backend: usually you will send chat messages using this method.
requestOlderMessages(data) Promise Method to ask your backend for older messages. Usually data will contain a timestamp or message index to serve as the offset to the time in the older message list where you want the backend to send messages to your chat widget.
on(event, callback) Nothing Method to let the frontend chat-widget subscribe to events emitted from your backend adapter. The chat-widget subscribes to the ucw:newRemoteMessage event that your adapter should emit when a new message is received from the backend.

To see how this interface was implemented for a Rails Action Cable backend, see this code: https://github.com/catogonzalez/chat-adapter-action-cable/blob/master/src/adapter.js

The easiest way to start implementing a backend is by cloning the repo of one of the existing Adapaters:

Clone this wiki locally