Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Websockets ws - Nodo y Express - API de exportación

Puse en funcionamiento correctamente mi websocket, pero necesito usar el método .send() en mi ruta.

Tengo mi aplicación dividida en varios archivos. En este caso obtuve:

aplicación.js

 //..some other code.. //Server const server = http.createServer(app) var port = process.env.PORT || 80; const connection = server.listen(port, () => { console.log('Listening on ' + port); }); const WebSocketServer = require("ws").Server; const wss = new WebSocketServer({ server }); //Here I need to export the wss module.exports.sengMsg = function (msg, callback) { return wss.on("connection", function (ws) { ws.send(msg, callback); ws.on("close", function () { console.log("websocket connection close") }) }) };

api.js

 //..some other code.. var sendMsg = require('../app').sendMsg; router.get('/', (req, res) => { sendMsg('Hi I from default route', (err)=>{ console.log("I got some error on ws"); }) res.send('Welcome to my API'); })

Pero al final se me ocurre

TypeError: sendMsg no es una función

¿Cuál es la forma recomendada de exportar la funcionalidad de mi wss en mis rutas usando esta configuración?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Intenta cambiar el código a,

 var sendMsg = require('./app'); router.get('/', (req, res) => { sendMsg.sendMsg('Hi I from default route', (err)=>{ console.log("I got some error on ws"); }) res.send('Welcome to my API'); })

o cambie app.js module.exports a siguiente,

 module.exports = { sendMessage :function (msg, callback) { return wss.on("connection", function (ws) { ws.send(msg, callback); ws.on("close", function () { console.log("websocket connection close") }) }) } }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!