Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
Hi,
is there a possibility to use socket.io in .lp file to create realtime and simple connection with client without a http requests?
Done is better than perfect
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
Recently I've created a nodejs app for realtime controlling inverter in one project. It had a client and a server and all data was exchanged via websockets in two directions. Thanks to websocket it was possible because like in LM with localbus. But in this project I've noticed that this technology could simplify and improve also other communication between client and server what's normally is done by http requests and exchanging data in JSON file.
I think websockets would be useful in .lp files for:
- every client show up-to-date app state (server sends every change in config to all clients without need to refresh page by client). This also removes the need for polling some data.
- server could send something to connected clients.
- generally I think that such possiblity could help to create better and more advanced apps to LM
Another question I have:
- could you replace jQuery need from localbus?
Done is better than perfect
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
Thanks admin, it is helpful. What is the websocket server path in this example (I must type it on client script)?
Done is better than perfect
Posts: 7773
Threads: 42
Joined: Jun 2015
Reputation:
447
As for any other .lp, just replace http:// with ws://
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
I try understand this and not only do this. So if in .lp file there would be 2 websockets defined then they would using same address?
Done is better than perfect
Posts: 7773
Threads: 42
Joined: Jun 2015
Reputation:
447
Why two websockets? There's one localbus socket which is local UDP broadcast, another one is server-client TCP connection.
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
18.12.2018, 17:35
(This post was last modified: 18.12.2018, 17:36 by buuuudzik.)
(18.12.2018, 16:57)admin Wrote: Why two websockets? There's one localbus socket which is local UDP broadcast, another one is server-client TCP connection.
This was only theoretically question. I don't understand where in attached code there is the websocket assignment to some port. Because in a few examples which I read always there was some address + port 8080 (probably default for websocket);
Like below:
Code:
local ev = require'ev'
-- create a copas webserver and start listening
local server = require'websocket'.server.ev.listen
{
-- listen on port 8080
port = 8080,
-- the protocols field holds
-- key: protocol name
-- value: callback on new connection
protocols = {
-- this callback is called, whenever a new client connects.
-- ws is a new websocket instance
echo = function(ws)
ws:on_message(function(ws,message)
ws:send(message)
end)
-- this is optional
ws:on_close(function()
ws:close()
end)
end
}
}
-- use the lua-ev loop
ev.Loop.default:loop()
Done is better than perfect
Posts: 7773
Threads: 42
Joined: Jun 2015
Reputation:
447
You don't need a separate port, websockets is just an extension over standard http requests.
Posts: 940
Threads: 161
Joined: Jul 2015
Reputation:
33
Thank you very much for your admin for help, I will test it.
Done is better than perfect