From b01491f3c75bff6cdef4ce207d2893898697eeb3 Mon Sep 17 00:00:00 2001 From: Thomas Kelly Date: Wed, 19 Jul 2017 15:01:15 -0400 Subject: [PATCH] add sockets --- client/index.js | 3 +++ client/socket.js | 9 +++++++++ package.json | 3 ++- server/index.js | 11 ++++++++--- server/socket/index.js | 10 ++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 client/socket.js create mode 100644 server/socket/index.js diff --git a/client/index.js b/client/index.js index 8d7f8da..009c5b1 100644 --- a/client/index.js +++ b/client/index.js @@ -5,6 +5,9 @@ import {Provider} from 'react-redux' import store from './store' import Routes from './routes' +// establishes socket connection +import socket from './socket' + ReactDOM.render( diff --git a/client/socket.js b/client/socket.js new file mode 100644 index 0000000..3ce6f5b --- /dev/null +++ b/client/socket.js @@ -0,0 +1,9 @@ +import io from 'socket.io-client'; + +const socket = io(window.location.origin); + +socket.on('connect', function () { + console.log('Connected!'); +}); + +export default socket diff --git a/package.json b/package.json index 7932a9b..7a6f688 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "redux": "^3.6.0", "redux-logger": "^2.8.1", "redux-thunk": "^2.2.0", - "sequelize": "^4.3.1" + "sequelize": "^4.3.1", + "socket.io": "^2.0.3" }, "devDependencies": { "babel-core": "^6.22.1", diff --git a/server/index.js b/server/index.js index 2a9de67..86fb4f5 100644 --- a/server/index.js +++ b/server/index.js @@ -9,6 +9,7 @@ const db = require('./db') const store = new SequelizeStore({db}) const PORT = process.env.PORT || 8080 const app = express() +const socketio = require('socket.io') module.exports = app /** @@ -74,12 +75,17 @@ const createApp = () => { app.use((err, req, res, next) => { res.status(err.status || 500).send(err.message || 'Internal server error.') }) + + // start listening (and create a 'server' object representing our server) + const server = app.listen(PORT, () => console.log(`Mixing it up on port ${PORT}`)) + + // set up our socket control center + const io = socketio(server) + require('./socket')(io) } const syncDb = () => db.sync() -const listenUp = () => app.listen(PORT, () => console.log(`Mixing it up on port ${PORT}`)) - // This evaluates as true when this file is run directly from the command line, // i.e. when we say 'node server/index.js' (or 'nodemon server/index.js', or 'nodemon server', etc) // It will evaluate false when this module is required by another module - for example, @@ -88,7 +94,6 @@ if (require.main === module) { store.sync() .then(syncDb) .then(createApp) - .then(listenUp) } else { createApp(app) } diff --git a/server/socket/index.js b/server/socket/index.js new file mode 100644 index 0000000..401491b --- /dev/null +++ b/server/socket/index.js @@ -0,0 +1,10 @@ +module.exports = (io) => { + + io.on('connection', (socket) => { + console.log(`A socket connection to the server has been made: ${socket.id}`) + + socket.on('disconnect', () => { + console.log(`Connection ${socket.id} has left the building`) + }) + }) +}