@adonisjs/websocket-client - import issue

#1

Hello,

I created a new Provider in AdonisJS and I want to connect to a websocket server which is running on the same Adonis instance as well.
Following the documentation I see I have to do the steps below:

npm i @adonisjs/websocket-client

Then Import the WebSocket client like so:

import Ws from '@adonisjs/websocket-client'
const ws = Ws('ws://localhost:3333')

The thing is that if I do this, I get an error:

SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' import Ws from '@adonisjs/websocket-client'

OK, I understand this is not a .ts file so I switch to the use notation.

const Ws = use('@adonisjs/websocket-client')
const ws = Ws('ws://localhost:3333')

Error:

SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' export default index;

What can I do?
I would like to push some message when an event occurs on the server and broadcast the information to all connected clients.

1 Like

#2

The websocket client is important in the client side code executed on the browser and not the AdonisJs server, so don’t expect use to work at all.

Regarding import error, it basically depends on the build tools you are using to compile your Javascript.

1 Like

#3

Thank you @virk, I understand.
Basically I would like to notify all clients via websocket that something happened on backend side, so I thought I could do something like pushing a message and broadcast to all clients. I understand that’s not the right way to achieve it.
What do you suggest?

0 Likes