Skip to content

Instantly share code, notes, and snippets.

@marshallswain
Created May 30, 2017 22:28
Show Gist options
  • Save marshallswain/1e521ff65ce4c676664f346ec1db5e3b to your computer and use it in GitHub Desktop.
Save marshallswain/1e521ff65ce4c676664f346ec1db5e3b to your computer and use it in GitHub Desktop.
Listening to user updates and updating the connected sockets
const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const authentication = require('./authentication');
const app = feathers();
// Load app configuration
app.configure(configuration(path.join(__dirname, '..')));
// Enable CORS, security, compression, favicon and body parsing
app.use(cors());
app.use(helmet());
app.use(compress());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));
// Host the public folder
app.use('/', feathers.static(app.get('public')));
// Set up Plugins and providers
app.configure(hooks());
app.configure(rest());
app.configure(socketio());
app.configure(authentication);
// Set up our services (see `services/index.js`)
app.configure(services);
app.service('users').on('patched', function (user) {
const userId = user._id || user.id
debugger
if (app.io) {
Object.keys(app.io.sockets.sockets).forEach(socketId => {
const socket = app.io.sockets.sockets[socketId]
const socketUser = socket.feathers && socket.feathers.user
if (socketUser) {
const socketUserId = socketUser._id || socketUser.id
if (userId === socketUserId) {
Object.assign(socketUser, user)
}
}
})
}
})
// Remove all users
app.service('users').remove(null, {})
.then(() => app.service('users').create({
email: '[email protected]'
}))
.then(user => {
return app.service('users').patch(user._id, { email: '[email protected]' })
})
.then(user => {
debugger
})
// Configure middleware (see `middleware/index.js`) - always has to be last
app.configure(middleware);
app.hooks(appHooks);
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment