Skip to content

Instantly share code, notes, and snippets.

@murprakoso
Created May 28, 2022 08:47
Show Gist options
  • Save murprakoso/508a00d614b744cd4af61188cd2af505 to your computer and use it in GitHub Desktop.
Save murprakoso/508a00d614b744cd4af61188cd2af505 to your computer and use it in GitHub Desktop.
socket io
const express = require('express')
const { createServer } = require('http')
const app = express()
const httpServer = createServer(app)
/** connect */
sockets.connect(httpServer)
sockets.emit('connection', { message: 'This is an event!' })
const { Server } = require('socket.io')
let io = null
module.exports = {
connect: function (httpServer) {
io = new Server(httpServer, {
cors: {
origin: '*',
},
})
},
emit: function (event, values) {
if (io) {
io.sockets.emit(event, values)
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment