EDIT: A solution has been found for this problem, and the finished project can be found here.
Enonic version: 7.5.0
OS: Ubuntu
Hi guys,
I’m building something really cool for Enonic Market, so please help me!
I’m doing a serverside XP-library that integrates with Turbo Streams (for more checkout this Full Stack Radio episode).
If I can get it working, we can do the following code serverside (e.g in a service triggered by AJAX):
turboLib.append({
target: 'my-alert-wrapper-id',
content: '<div role="alert">Something went wrong</div>'
});
And a websocket will be used to send the markup to the fontend, and append it to the element with #my-alert-wrapper-id
, and there is no need for any frontend JS-code!
This approach can potentially replace much frontend JS-code. Or just be a way to do websockets, without having to write any frontend JS-code.
Problem:
When I’m in a service (called by AJAX), how do I know which websocket (socketId
) is connected to which users client webpage?
This is my code so far:
Here is what I have tried:
- In
webSocketEvent()
ontype
=open
, store thesocketId
on the users session using lib-xp-session. The session is not available inside the websocket, so this is currently not possible. - Use a shared
object
to create a map fromsessionId
tosocketId
. ThesessionId
can be passed in towebSocketEvent()
troughevent.data
.
UnfortionatlywebSocketEvent()
doesn’t share memory with http-services, so this map is not accessible outsidewebSocketEvent()
.
So how can I easily communicate the id of a socket belonging to a user outside webSocketEvent()
?