Support for Qt QML JavaScript?

Hi,

I’m trying to use phoenix.js in my Qt QML project which has it’s own buildin JavaScript engine. Problem is that (what I googled so far) that this engine has cut out WebSocket class, because QML has its own WebSocket component. When I import phoenix.js library and try to create Socket then I get error:

TypeError: Cannot read property 'WebSocket' of undefined

Problem is probably in this line:

this.transport = opts.transport || global.WebSocket || LongPoll;

So in theory, it is possible to inject QML WebSocket{} engine in phoenix.js by Qt.createQmlObject() or Qt.createComponent(). But this probably require change names of original WebSocket’s method names and signals etc. in phoenix.js file.
Did anyone tried this? I’m still newbie in QML and JavaScript, as C++ and Qt Widgetsets programmer, I’m trying to make new project using QML framework so I’m not such experienced to do it by my self.
Regards

Hi and welcome :slight_smile:

How are you initializing your socket?
The phoenix.js docs have a list of options that you can pass when initializing it and transport is one of the options which would allow you to override (as you can see in the line that you highlighted as the source of the error).

Hi,
Thanks for reply.
I’m importing phoenix in my QML by this:

import "phoenix.js" as MyScript

And creating:

let socket = new MyScript.Phoenix.Socket("ws://localhost:4000/socket", {params: {token: "test_token"}})

I tried now with:

let socket = new MyScript.Phoenix.LongPoll("ws://localhost:4000/socket", {params: {token: "test_token"}})

But getting error:

phoenix.js:639: ReferenceError: setTimeout is not defined

I remember that this Qt JavaScript environment is a pita to work with…

What are you trying to accomplish? You want to interact and display a remote LiveView application? Or do you want to communicate with a Phoenix channel from within your Qt application?

Well. My friend is writting kind of chat (with rooms etc) using phoenix serwer. Beside WebApp client, we want to also have lightweight multiplatform desktop app which also cover mobiles. As a desktop programmer who have some experience with Qt Widgetsets C++ framework, decided to use Qt Quick QML framework to gain practice

If you’re only interested in communicating through a channel, then you don’t really need to use the javascript runtime. I think there are implementations in C and C++ to connect to a Phoenix Channel (can’t find it right now, so if anyone knows where to find those other language bindings, pls share).

If you want to display a LV, then you’ll have to solve this indeed. Or use another way to package your application. There are a few other ways to package a Phoenix app for desktop. Check LiveView Native for mobile.

Thanks for hints. I’ll do some research this evening. If indeed there is some unofficial C++ wrapper for phoenix then I can still use it in QML, because C++ is backend for QML and you can share C++ objects between Qt JavaScript and backend

https://hexdocs.pm/phoenix/channels.html#client-libraries

https://hexdocs.pm/phoenix/channels.html#client-libraries

There is no wrappers for C/C++ :frowning: . Seems that I must search for some unofficial

Look like there is no good C++ wrappers for phoenix. This one wasn’t updated from 7 years so I guess it will not work