javascript - Get request parameters inside socket -


i want create new document in mongodb collection every time socket run, these collections should belong page @ at.

right hardcording pageid this

io.on('connection', (socket) => {   const pageid = '57cc491c95f2513e5aad2590';    socket.on('new nodes', (positions) => {     document.create({pageid: pageid}, (err, doc) => {       // ...     });   }); }); 

but want id req.params.pageid inside express route

app.route('/pages/:pageid').get((req, res) => {   // ... }); 

the socket.io connection stands on own , isn't part of express route handler. so, can't directly access req.params.pageid socket.io connection because request on , done when socket.io connection arrives. so, you're going have include additional information either when connect socket.io or when send particular socket.io message indicates page id.

so, have couple of choices:

  1. when connect page, can include query parameter on connect url indicates pageid. then, in io.on('connection', ...) code, can @ query parameter pageid.

  2. when send new nodes message, can include data argument specifies pageid.

this can either implemented in javascript on page figures out pageid , inserts desired parameter or can done more automatically whatever rendering engine creates initial page (so pageid inserted @ page render time directly code).


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -