question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Socket.io with Ltijs

See original GitHub issue

As I am currently using Ltijs for an app that uses sockets, specifically Socket.io, I would need to access the server object. for example, var server = app.listen(4000, ... so that I can create a socket, var io = socketIO(server). I have looked at the source code for ltijs, and in the Server.js file, I see that for the listen function,

  listen (port) {
    return new Promise((resolve, reject) => {
      if (this.ssl) {
        this.server = https.createServer(this.ssl, this.app).listen(port)
      } else {
        this.server = this.app.listen(port)
      }
      this.server.on('listening', () => {
        resolve(true)
      })
      this.server.on('error', (err) => {
        reject(err)
      })
    })
  }

the server object is being assigned as a class variable called server

In the Provider.js file,

    this.#server = new Server(options ? options.https : false, options ? options.ssl : false, this.#ENCRYPTIONKEY, options ? options.cors : true, options ? options.serverAddon : false)

    /**
     * @description Express server object.
     */
    this.app = this.#server.app

only the app, as in app = express() is exposed. However, I need access to the server object so ideally, this.server = this.#server.server so that I can run Socket.io within the same HTTP server instance, var io = socketIO(server). Thank you for looking into this. Heres a resource link that explains more: https://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
vandergavcommented, Apr 16, 2021

Ok thanks for the tip. I’ll probably need to look into this more. I’ll update again when things become clearer thanks again.

Update: I was using app.use(cookieParser()); in my app without passing a secret to it. Changed it to app.use(cookieParser(options.moodle.LTI_KEY));, aka passed the LTI_KEY into cookieParser(), and it works now. Thanks so much for this @Cvmcosta

1reaction
Cvmcostacommented, Apr 16, 2021

Probably, are you passing a secret to cookieParser? I believe that for consistency you should pass the LTI_KEY to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction | Socket.IO
Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. Diagram of a ...
Read more >
Introduction | Socket.IO
Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of: a ...
Read more >
How it works | Socket.IO
... or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.
Read more >
Server Installation | Socket.IO
By default, Socket.IO use the WebSocket server provided by the ws package. There are 2 optional packages that can be installed alongside this ......
Read more >
Client usage with bundlers - Socket.IO
To use the client in a Node.js environment (server to server connection), here is the configuration: Installation: npm i -D socket.io-client ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found