Skip to content

Commit

Permalink
add sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkelly28 committed Jul 19, 2017
1 parent 532f5bd commit b01491f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Provider store={store}>
<Routes />
Expand Down
9 changes: 9 additions & 0 deletions client/socket.js
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 8 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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,
Expand All @@ -88,7 +94,6 @@ if (require.main === module) {
store.sync()
.then(syncDb)
.then(createApp)
.then(listenUp)
} else {
createApp(app)
}
10 changes: 10 additions & 0 deletions server/socket/index.js
Original file line number Diff line number Diff line change
@@ -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`)
})
})
}

0 comments on commit b01491f

Please sign in to comment.