Skip to content

Instantly share code, notes, and snippets.

@maxgfr
Last active April 8, 2020 16:02
Show Gist options
  • Save maxgfr/0247555ca90a44b355bbebe89bcb5c92 to your computer and use it in GitHub Desktop.
Save maxgfr/0247555ca90a44b355bbebe89bcb5c92 to your computer and use it in GitHub Desktop.
Socket.io + Express JS + React JS
var socket = require('socket.io');
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
const server = app.listen(process.env.PORT || '80', () => console.log(`App is listening on port ${process.env.PORT || '80'}!`));
const io = socket(server);
io.on('connection', (socket) => {
socket.on('send_event_to_front', (data) => {
io.emit('new_message', data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment