Socket.io

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

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

The @feathersjs/socketio module allows to call service methods and receive real-time events via Socket.io, a NodeJS library which enables real-time bi-directional, event-based communication.

Important: This page describes how to set up a Socket.io server. The Socket.io client chapter shows how to connect to this server on the client and the message format for service calls and real-time events.

Configuration

@feathersjs/socketio can be used standalone or together with a Feathers framework integration like Express.

app.configure(socketio())

Sets up the Socket.io transport with the default configuration using either the server provided by app.listen or passed in app.setup(server).

  1. const feathers = require('@feathersjs/feathers');
  2. const socketio = require('@feathersjs/socketio');
  3. const app = feathers();
  4. app.configure(socketio());
  5. app.listen(3030);

Pro tip: Once the server has been started with app.listen() or app.setup(server) the Socket.io object is available as app.io.

app.configure(socketio(callback))

Sets up the Socket.io transport with the default configuration and call callback with the Socket.io server object. This is a good place to listen to custom events or add authorization:

  1. const feathers = require('@feathersjs/feathers');
  2. const socketio = require('@feathersjs/socketio');
  3. const app = feathers();
  4. app.configure(socketio(function(io) {
  5. io.on('connection', function(socket) {
  6. socket.emit('news', { text: 'A client connected!' });
  7. socket.on('my other event', function (data) {
  8. console.log(data);
  9. });
  10. });
  11. // Registering Socket.io middleware
  12. io.use(function (socket, next) {
  13. // Exposing a request property to services and hooks
  14. socket.feathers.referrer = socket.request.referrer;
  15. next();
  16. });
  17. }));
  18. app.listen(3030);

app.configure(socketio(options [, callback]))

Sets up the Socket.io transport with the given Socket.io options object and optionally calls the callback described above.

This can be used to e.g. configure the path where Socket.io is initialize (socket.io/ by default). The following changes the path to ws/:

  1. const feathers = require('@feathersjs/feathers');
  2. const socketio = require('@feathersjs/socketio');
  3. const app = feathers();
  4. app.configure(socketio({
  5. path: '/ws/'
  6. }, function(io) {
  7. // Do something here
  8. // This function is optional
  9. }));
  10. app.listen(3030);

app.configure(socketio(port, [options], [callback]))

Creates a new Socket.io server on a separate port. Options and a callback are optional and work as described above.

  1. const feathers = require('@feathersjs/feathers');
  2. const socketio = require('@feathersjs/socketio');
  3. const app = feathers();
  4. app.configure(socketio(3031));
  5. app.listen(3030);

params

Socket.io middleware can modify the feathers property on the socket which will then be used as the service call params:

  1. app.configure(socketio(function(io) {
  2. io.use(function (socket, next) {
  3. socket.feathers.user = { name: 'David' };
  4. next();
  5. });
  6. }));
  7. app.use('messages', {
  8. create(data, params, callback) {
  9. // When called via SocketIO:
  10. params.provider // -> socketio
  11. params.user // -> { name: 'David' }
  12. }
  13. });

params.provider

For any service method call made through Socket.io params.provider will be set to socketio. In a hook this can for example be used to prevent external users from making a service method call:

  1. app.service('users').hooks({
  2. before: {
  3. remove(context) {
  4. // check for if(context.params.provider) to prevent any external call
  5. if(context.params.provider === 'socketio') {
  6. throw new Error('You can not delete a user via Socket.io');
  7. }
  8. }
  9. }
  10. });

params.query

params.query will contain the query parameters sent from the client.

Important: Only params.query is passed between the server and the client, other parts of params are not. This is for security reasons so that a client can’t set things like params.user or the database options. You can always map from params.query to params in a before hook.

params.connection

params.connection is the connection object that can be used with channels. It is the same object as socket.feathers in a Socket.io middleware as shown in the params section.

uWebSocket

The options can also be used to initialize uWebSocket which is a WebSocket server implementation that provides better performace and reduced latency.

  1. $ npm install uws --save
  1. const feathers = require('@feathersjs/feathers');
  2. const socketio = require('@feathersjs/socketio');
  3. const app = feathers();
  4. app.configure(socketio({
  5. wsEngine: 'uws'
  6. }));
  7. app.listen(3030);