A client for Feathers services supporting many different transport libraries.
feathers-client is a small module that lets you use remote Feathers services relying on any of the following libraries:
- REST API
- Websockets (with real-time updates)
feathers-client is used much the same way as you would use Feathers on the server making it seamless to use in other NodeJS applications or in the browser. With NodeJS or Browserify:
npm install feathers-client
var feathers = require('feathers-client');The dist/feathers.js file also provides a UMD version that works with most module loaders or standalone (providing a feathers global name).
<script type="text/javascript" src="node_modules/feathers-client/dist/feathers.js"></script>var app = feathers('http://todos.feathersjs.com')
.configure(feathers.socketio());
var todoService = app.service('todos');
todoService.on('created', function(todo) {
console.log('Todo created', todo);
});
todoService.create({
text: 'A todo',
complete: false
}, function(error, todo) {
console.log('Success');
});
todoService.find(function(error, todos) {
console.log('Got the following Todos', todos);
});Connecting to a Feathers service via the REST API is possible using jQuery, request or Superagent:
Important: REST client services do emit created, updated, patched and removed events but only locally for their own instance. Real-time events from other clients can only be received by using a websocket connection.
jQuery $.ajax needs the API base URL and an instance of jQuery passed to feathers.jquery. If no jQuery instance is passed the global jQuery will be used.
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.jquery());The request object needs to be passed explicitly to feathers.request. Using request.defaults - which creates a new request object - is a great way to set things like default headers or authentication information:
var request = require('request');
var client = request.defaults({
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.request(client));Superagent currently works with a default configuration:
var superagent = require('superagent');
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.superagent(superagent));Websocket real-time connections can be established via Socket.io or Primus. Websocket services emit all events that they receive allowing you to implement real-time functionality.
Provide either a connected socket or the URL of the websocket endpoint:
var socket = io('http://todos.feathersjs.com');
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.socketio(socket))
// or
.configure(feathers.socketio('http://todos.feathersjs.com'))Websocket connections are also very efficient for real-time communication between different NodeJS servers. First install socket.io-client:
npm install socket.io-client
Then pass the connection just like in the browser:
var io = require('socket.io-client');
var socket = io('http://todos.feathersjs.com');
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.socketio(socket));Primus works similar to Socket.io:
<script type="text/javascript" src="node_modules/feathers-client/dist/feathers.js"></script>
<script type="text/javascript" src="primus/primus.js"></script>
<script type="text/javascript">
var primus = new Primus('http://todos.feathersjs.com');
var app = feathers('http://todos.feathersjs.com')
.configure(feathers.primus(primus));
</script>0.1.0
- Initial release
Copyright (c) 2015 David Luecke
Licensed under the MIT license.
