Created
May 28, 2022 08:47
-
-
Save murprakoso/508a00d614b744cd4af61188cd2af505 to your computer and use it in GitHub Desktop.
socket io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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