RxWS
RxWS is a RESTful reactive JavaScript implementation on top of web sockets. This includes,
GET
, POST
, PUT
, REMOVE
(DELETE), PATCH
, and HEAD
. RxWS guarantees message delivery by generating
a correlation id for each message (to and from the server). Both the server and client automatically send an
acknowledgement response for each request. If there is no acknowledgement after a timeout, an error is thrown.
RxWS implements a RESTful protocol. You can use any websocket server as long as it implements the same protocol. By default RxWS supports SocketIO with rxws-socketio
Setup
RxWS requires a websocket abstraction layer. By default it supports both SockJS and SocketIO.
import rxws from 'rxws';
import SocketIOBackend from 'rxws-socketio/SocketIOBackend';
rxws.setBackend({
backend: SocketIOBackend,
url: 'ws://someurl'
});
rxws.get('users')
.subscribe(data => console.log)
.catch(error => console.error)
Example
Performing a GET
request:
rxws.get('users')
.subscribe(data => console.log, error => console.error)
rxws.get('users', {
parameters: { 'users': 13 }
})
.subscribe(data => console.log, error => console.error)
rxws({
method: 'get',
resource: 'users',
parameters: { 'users': 13 }
})
.subscribe(data => console.log, error => console.error)
Performing a POST
request:
rxws.post('users', {
firstName: 'Johnny',
lastName: 'Appleseed'
})
.subscribe(data => console.log, error => console.error)
rxws({
method: 'post',
resource: 'users',
body: {
firstName: 'Johnny',
lastName: 'Appleseed'
}
})
.subscribe(data => console.log, error => console.error)
Nested resources:
rxws.get('posts.comments', {
parameters: { 'posts': 13, 'comments': 15 }
})
.subscribe(data => console.log, error => console.error)
Custom headers:
rxws.get('posts.comments', {
parameters: { 'posts': 13 },
apiVersion: '1.2.1',
accessToken: '7fgnasdfvy0afdsjfjdls',
queryParameters: { include: 'history' }
})
.subscribe(data => console.log, error => console.error)
Server Notifications:
rxws.onNotification('newPost')
.forEach((messageBody) => {
rxws.get('posts', { parameters: { posts: messageBody.id } })
.subscribe(data => console.log, error => console.error);
})
Request Middleware:
rxws.requestUse()
.subscribe(({req, send, reply, next}) => {
req.header.resource = 'prefix.' + req.header.resource;
next();
}, ({req, err}) => {
});
Response Middleware:
rxws.use()
.subscribe(({req, res, reply, retry, next}) => {
res.requestTime = Date.now();
next();
});
rxws.use()
.subscribe(({req, res, reply, retry, next}) => {
next();
}, ({req, err}) => {
});
rxws.use()
.subscribe(({req, res, reply, retry, next}) => {
reply(res);
});
rxws.use()
.subscribe(({res, reply, retry, next}) => {
if (res.header.statusCode === 401) {
auth.refreshAuthToken()
.then(() => retry())
} else {
reply(res);
}
})
Reactive example:
var source = rxws.get('url').retry(3).catch(cachedVersion());
var subscription = source.subscribe(
(data) => {
console.log(data);
}
);
API
rxws.setBackend(options)
rxws.setBackend({
backend: rxwsBackendImplementation,
url: string,
url: (): Observable,
defaultHeaders?: object,
requestTransformer?: (request: object, send: Function): null,
responseTransformer?: (response: object, reply: Function, retry: Function): null,
timeout?: 10000,
onConnectionError?: (error: string): null
})
rxws(config): observable
rxws({
method: 'get',
resource: 'posts',
parameters: { 'posts': 13 }
});
rxws.get(resource[, config]): observable
rxws.delete(resource[, config]): observable
rxws.head(resource[, config]): observable
rxws.post(resource[, data[, config]]): observable
rxws.put(resource[, data[, config]]): observable
rxws.patch(resource[, data[, config]]): observable
rxws.onMessage(type: string): observable
config obbject:
{
resource: string,
method: string,
parameters: object,
data: object,
extraResources: object,
queryParameters: object
}
###License
ISC License (ISC)
Copyright (c) 2016, CanopyTax
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.