canjs / can-connect-feathers

The FeathersJS client library for DoneJS and can-connect
https://canjs.com/doc/can-connect-feathers.html
MIT License
10 stars 4 forks source link

Switch sockets to XHRs on the SSR server #20

Closed marshallswain closed 7 years ago

marshallswain commented 7 years ago

Socket.io performs a long upgrade process to get the actual WebSocket initially connected (unless you're only using the WebSocket transport, which isn't usually recommended for most apps). So using Socket.io on the server takes MUCH longer than simply firing XHR requests.

This zone plugin will need to convert socket calls to proper XHR requests. The existing XHR-zone is already handling Authorization headers for JWTs, so this shouldn't need to worry about auth.

Doing this in can-zone is probably more complicated than needed.

Current API for setting up a Feathers client looks like this:

import feathers from 'feathers/client';
import socketio from 'feathers-socketio';
import io from 'steal-socket.io';
import auth from 'feathers-authentication/client';
import hooks from 'feathers-hooks';

var socket = io('http://localhost:3030', {
  transports: ['websocket']
});

const app = feathers()
  .configure(socketio(socket))
  .configure(hooks())
  .configure(auth());

export default app;

The only change necessary will be switching the import from feathers-socketio to feathers-socketio-ssr:


// To this:
import feathers from 'feathers/client';
import socketio from 'feathers-socketio-ssr';
import io from 'steal-socket.io';
import auth from 'feathers-authentication/client';
import hooks from 'feathers-hooks';

var socket = io('http://localhost:3030', {
  transports: ['websocket']
});

const app = feathers()
  .configure(socketio(socket))
  .configure(hooks())
  .configure(auth());

export default app;
marshallswain commented 7 years ago

Published to npm as feathers-socketio-ssr.