Adonis 4.1 websocket config


#1

So I upgraded to 4.1 and everything is working great, then saw in the docs you have added websocket support so I tried to give it a shot.

Following the docs I installed the websocket library. Added the provider in app.js. Added the .wsServer() in server.js and then the docs say:

Also, the addon will attempt to create the following files inside your project.
config/socket.js file contains the server configuration.
start/socket.js is used to boot the socket server and register Channels.
start/wsKernel.js is used to register middleware to be executed on channel subscriptions.

Now those files did not show up for me and the docs do not really state what to do if they don’t so I am at a bit of a loss. Taking a peak at: https://github.com/adonisjs/adonis-websocket/blob/v1.0.9/instructions.md it seems like this might be what I am supposed to do. Though it doesn’t mention the config/socket.js, but maybe this is it: https://github.com/adonisjs/adonis-websocket/blob/v1.0.9/config/socket.js.

Any help would be appreciated!


#2

Well after experimenting for awhile I have found in fact that my guesses to follow the adonis-websocket repo instructions and config turned out to work. After some experimenting I got websockets up and working!


#3

how to do that?

I just try to copy the config/socket.js to from the adonis-websocket to my project but it doesn’t work


#4

So I have not touched this code since around June so it is a bit fuzzy at this point. Here is roughly what I remember:

  • Follow this: https://adonisjs.com/docs/4.1/websocket
    • Add .wsServer() to server.js
    • Add ‘@adonisjs/websocket/providers/WsProvider’ to providers in app.js
    • Add wsKernel.js
    • (optional i think) add socket.js
    • Then what I did is in my Api controller when someone saved a record I broadcast a list of rows.
    const channel = Ws.getChannel('draftOrder');
    const topic = channel.topic('draftOrder');

    if(topic !== null) {
            const draftOrderRows = await DraftOrder
              .query()
              .where('league_id', '=', leaguePlayer.league_id)
              .with('leaguePlayer')
              .with('leaguePlayer.player')
              .with('leaguePlayer.player.positions')
              .fetch();

            topic.broadcast('message', draftOrderRows);
          }