Web socket - persistent connection

Is this possible to connect from losant (workflow) to web socket and maintain persistent connection for unlimited time?

Best regards.
Mirek.

1 Like

Opening a persistent websocket connection using a workflow is not possible. Cloud workflows have a 60 second timeout on any single execution. We have considered a websocket integration that would allow you to form a persistent connection and then trigger workflows on incoming messages. Would that work for your use case?

That would work for some of our use cases.

First of all thank you very much for fast answer,
I think it would work and at least it is worth to try. What I want to do is make a http request (undefined time) and wait for message, as you Brandon wrote above. Then, when message come I would trigger a workflow. Yes it sounds reasonably.
Sorry for my english.
Have a nice day.
Mirek.

@paul_wareham @Mirek_Gadomski

One of the challenges with a Websocket integration is that the data is raw bytes, so we canā€™t automatically know where a message ends to trigger a workflow with it.

Do either of you use a standard protocol on top of Websockets? Something like socket.io?

In an application context, we use J1939 (from the engine world) in Losant as a top level protocol and parse it within the platform, which is carried over a proprietary UDP-based protocol from the cellular gateway vendor.

We have not used either of these protocols with Websocket however. sockets.io seems like a great starting point though.

Hello Brandon, Evan.
Iā€™m not sure that my knowledge is enough deep to pass valuable
information to you. In few sentences Iā€™ll describe what I need to do.
Controller that I use is Seeed Studio Wio Node/Link with button connected to
it. Controller has predefined firmware that sends data to server with REST API
service. The benefit is that there is no need to program the controller. Seeed
Studioā€™s server doesnā€™t keep any historical data, so I have to pick it up
before it gone. Some information (data) for example temperature, humidiy is
easy to collect. Its enough to make http request every one minute or so. But
there is also event type data, generated by, for example: button or PIR sensor.
That event is passed by websocket. Below is example of connection in javascript:

image

The response message is json formatted and looks like below:

[5] RECV: {ā€œmsgā€: {ā€œbutton_pressedā€: ā€œ3ā€}}
[4] RECV: {ā€œmsgā€: {ā€œbutton_pressedā€: ā€œ3ā€}}
[3] RECV: {ā€œmsgā€: {ā€œbutton_pressedā€: ā€œ3ā€}}
[2] RECV: {ā€œmsgā€: {ā€œbutton_pressedā€: ā€œ3ā€}}
[1] SENT: ā€¦ā€¦your tokenā€¦ā€¦.
[0] CONNECTED

Best regards.
Mirek.

Please do elaborate on socket.io

How can it be used within the Losant Environment?

The way I need to use it is as follows:

  1. Create a socket connection useing a two-way communication (pushing information from Losant to and API)

1.1 Where can I plug the following socket connection code:

const Client = require(ā€˜socket.io-clientā€™);
socket = Client(SOCKET_SERVER_URL, { jsonp: false });
socket.on(ā€˜connect_errorā€™, (err) => {
console.log(err);
throw ā€˜Socket connection damaged, please checkā€™;
});

  1. Create auth event to authenticate the APIā€™s client id and client password (establish socket stability). Get a uth event for successful authentication.
    2.2 Where can I plug the following verification code:
    socket.emit(ā€˜authā€™, {
    client_id: ā€˜CLIENT_IDā€™,
    client_secret: ā€˜CLIENT_PASSWORDā€™,
    });
    socket.on(ā€˜authā€™, (data) => {
    console.log(data);
    });

2.2 Where will I be able to read the successful/failed authentication (JSON structure) for the auth event

  1. Push the data through the established socket.io once every X [min] with the event name: reading
    3.1 Where can I plug the following data push code:
    socket.emit(ā€˜readingsā€™,{
    ā€˜message_idā€™: ā€˜<MESSAGE_ID>ā€™,
    ā€˜dataā€™: {
    ā€˜Device_IDā€™: ā€˜ā€™,
    ā€˜readingsā€™: [{
    ā€˜Device_IDā€™: ā€˜ā€™,
    ā€˜reading_timeā€™:ā€˜epoch_valueā€™,
    ā€˜Name_of_Attribute_1ā€™: ā€˜numerical_valueā€™,
    ā€˜Name_of_Attribute_2ā€™: ā€˜numerical_valueā€™,
    ā€˜Name_of_Attribute_3ā€™: ā€˜numerical_valueā€™,
    ā€˜Name_of_Attribute_4ā€™: ā€˜numerical_valueā€™,
    ā€˜Name_of_Attribute_5ā€™: ā€˜numerical_valueā€™
    }]
    },
    });
    socket.on(ā€˜errorsā€™, (data) => {
    console.log(data);
    });
    socket.on(ā€˜ackā€™, (data) => {
    console.log(data);
    });

3.2 Where will I be able to validate the following (JSON structure) ack event?
{
ā€œcodeā€, ā€œā€,
ā€œackā€: true,
ā€œidā€: ā€œ<MESSAGE_ID>ā€
}

socket.io is not supported directly with Losant. Since this post was original created, however, we have released Streaming Endpoints, which utilizes Server-Sent Events under the hood.

Server-sent events provide real-time streaming of data in one direction: from the server to the client (usually a web browser).

To send data from the client to the server, a basic POST request to a standard Experience Endpoint works very well.

Thank you for this answer, the only way for me to communicate with this API is through socket.io
What will be your recommendation for a work around?
Should I send the payload to AWS and create a socket.io with their services: socket.io server under AWS ELB (Application Load Balancer) or Amazon API Gateway