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