Skip to content

Instantly share code, notes, and snippets.

@pnakibar
Last active October 3, 2016 04:34
Show Gist options
  • Save pnakibar/bca54a06e4c093626e787e23ae1812c6 to your computer and use it in GitHub Desktop.
Save pnakibar/bca54a06e4c093626e787e23ae1812c6 to your computer and use it in GitHub Desktop.
Express.js em tempo real com Socket.io
import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import socketio from 'socket.io'; // importando o socket.io
import http from 'http'; // talvez no seu projeto você não importe a biblioteca http, mas aqui é necessário
const app = express();
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', (req, res) => res.json({ hello: 'world!' })); // apenas para verificar se está tudo OK
const server = http.createServer(app); // a biblioteca http entra exatamente aqui
const io = socketio(server); // associando a instância do socketio com o seu servidor
io.on('connection', () => console.log('someone connected')); // imprimindo no console caso alguém entre
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`[x] Magic happens on port: ${port}`));
export default app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment