Feathers Client

One of the most notable features of Feathers is that it can also be used as the client. In contrast with most other frameworks, it isn’t a separate library; instead you get the exact same functionality with a client and on a server. This means you can use services and hooks and configure plugins. By default, a Feathers client automatically creates services that talk to a Feathers server.

In order to connect to a Feathers server, a client creates Services that use a REST or websocket connection to relay method calls and allow listening to events on the server. This means the Feathers application instance is usable the exact same way as on the server.

Modules most relevant on the client are:

Important: You do not have to use Feathers on the client to connect to a Feathers server. The client chapters above also describe how to use a REST HTTP, Socket.io or Primus connection directly without Feathers on the client side. For details on authentication, see the Authentication client chapter.

This chapter describes how to set up Feathers as the client in Node, React Native and in the browser with a module loader like Webpack or Browserify or through a <script> tag. The examples are using the Socket.io client. For other connection methods see the chapters linked above.

Important: Feathers can be used on the client through the individual modules or the @feathersjs/client module. The latter combines all modules mentioned above into a single, ES5 transpiled version.

Node

To connect to a Feathers server in NodeJS, install the desired client connection library (here, socket.io-client), alongside the Feathers core library, and the connection-specific library:

  1. npm install @feathersjs/feathers @feathersjs/socketio-client socket.io-client --save

Then initialize like this:

  1. const io = require('socket.io-client');
  2. const feathers = require('@feathersjs/feathers');
  3. const socketio = require('@feathersjs/socketio-client');
  4. const socket = io('http://api.my-feathers-server.com');
  5. const client = feathers();
  6. client.configure(socketio(socket));
  7. const messageService = client.service('messages');
  8. messageService.on('created', message => console.log('Created a message', message));
  9. // Use the messages service from the server
  10. messageService.create({
  11. text: 'Message from client'
  12. });

React Native

React Native usage is the same as for the Node client. Install the required packages into your React Native project.

  1. $ npm install @feathersjs/feathers @feathersjs/socketio-client socket.io-client

Then in the main application file:

  1. import io from 'socket.io-client';
  2. import feathers from '@feathersjs/feathers';
  3. import socketio from '@feathersjs/socketio-client';
  4. const socket = io('http://api.my-feathers-server.com', {
  5. transports: ['websocket'],
  6. forceNew: true
  7. });
  8. const client = feathers();
  9. client.configure(socketio(socket));
  10. const messageService = client.service('messages');
  11. messageService.on('created', message => console.log('Created a message', message));
  12. // Use the messages service from the server
  13. messageService.create({
  14. text: 'Message from client'
  15. });

Since React Native for Android doesn’t handle timeouts exceeding one minute, consider setting lower values for pingInterval and pingTimeout of feathers-socketio on your server. This which will stop warnings related to this issue. For example:

  1. const app = feathers();
  2. const socketio = require('feathers-socketio');
  3. app.configure(socketio({
  4. pingInterval: 10000,
  5. pingTimeout: 50000
  6. }));

Module loaders

All modules in the @feathersjs namespace are using ES6. They must be transpiled to support browsers that don’t completely support ES6. Most client-side module loaders exclude the node_modules folder from being transpiled and have to be configured to include modules in the @feathersjs namespace.

Webpack

For Webpack, the recommended babel-loader rule normally excludes everything in node_modules. It has to be adjusted to skip node_modules/@feathersjs. In the module rules in your webpack.config.js, update the babel-loader section to this:

  1. {
  2. test: /\.jsx?$/,
  3. exclude: /node_modules(\/|\\)(?!(@feathersjs))/,
  4. loader: 'babel-loader'
  5. }

create-react-app

create-react-app uses Webpack but does not allow to modify the configuration unless you eject. If you do not want to eject, use the @feathersjs/client module instead.

  1. npm i --save @feathersjs/client

You can then import the transpiled libraries from this package:

  1. import feathers from "@feathersjs/client";

Browserify

In Browserify the babelify transform must be used. All Feathers packages indicate that they need the transform and should be transpiled automatically.

Others

As mentioned above, node_modules/@feathersjs and all its subfolders must be included in the ES6+ transpilation step when using any module loader that is using a transpiler. For non-CommonJS formats (like AMD) and an ES5 compatible version of Feathers and its client modules you can use the @feathersjs/client module.

@feathersjs/client

GitHub stars
@feathersjs/client"">npm version
Changelog

  1. $ npm install @feathersjs/client --save

@feathersjs/client is a module that bundles the separate Feathers client-side modules into one providing the code as ES5 which is compatible with modern browsers (IE10+). It can also be used directly int the browser through a <script> tag. Here is a table of which Feathers client module is included:

Feathers module @feathersjs/client
@feathers/feathers feathers (default)
@feathers/errors feathers.errors
@feathersjs/rest-client feathers.rest
@feathersjs/socketio-client feathers.socketio
@feathersjs/primus-client feathers.primus
@feathersjs/authentication-client feathers.authentication

Important: The Feathers client libraries come transpiled to ES5 and require ES6 shims either through the babel-polyfill module or by including core.js in older browsers e.g. via <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/core-js/2.1.4/core.min.js"></script>

Important: When you are loading @feathersjs/client you do not have to install or load any of the other modules listed in the table above.

When to use

@feathersjs/client can be used directly in the browser using a <script> tag without a module loader as well as with module loaders that do not support CommonJS (like RequireJS) or React applications created with a default create-react-app.

If you are using the Feathers client with Node or React Native you should follow the steps described in the Node and React Native sections and not use @feathersjs/client.

Note: All Feathers client examples show direct usage and usage with @feathersjs/client.

Load with a module loader

You can use @feathersjs/client with other browser module loaders/bundlers (instead of using the modules directly) but it may include packages you may not use and result in a slightly larger bundle size.

  1. import io from 'socket.io-client';
  2. import feathers from '@feathersjs/client';
  3. // Socket.io is exposed as the `io` global.
  4. const socket = io('http://localhost:3030');
  5. // @feathersjs/client is exposed as the `feathers` global.
  6. const app = feathers();
  7. app.configure(feathers.socketio(socket));
  8. app.configure(feathers.authentication());
  9. app.service('messages').create({
  10. text: 'A new message'
  11. });
  12. // feathers.errors is an object with all of the custom error types.

Load from CDN with <script>

Below is an example of the scripts you would use to load @feathersjs/client from unpkg.com.

  1. <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/core-js/2.1.4/core.min.js"></script>
  2. <script src="//unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js"></script>
  3. <script src="//unpkg.com/socket.io-client@1.7.3/dist/socket.io.js"></script>
  4. <script>
  5. // Socket.io is exposed as the `io` global.
  6. var socket = io('http://localhost:3030');
  7. // @feathersjs/client is exposed as the `feathers` global.
  8. var app = feathers();
  9. app.configure(feathers.socketio(socket));
  10. app.configure(feathers.authentication());
  11. app.service('messages').create({
  12. text: 'A new message'
  13. });
  14. // feathers.errors is an object with all of the custom error types.
  15. </script>

RequireJS

Here’s how to load feathers-client using RequireJS Syntax:

  1. define(function (require) {
  2. const feathers = require('@feathersjs/client');
  3. const { socketio, authentication } = feathers;
  4. const io = require('socket.io-client');
  5. const socket = io('http://localhost:3030');
  6. // @feathersjs/client is exposed as the `feathers` global.
  7. const app = feathers();
  8. app.configure(socketio(socket));
  9. app.configure(authentication());
  10. app.service('messages').create({
  11. text: 'A new message'
  12. });
  13. return const;
  14. });