question

Axel Kohonen avatar image
3 Likes"
Axel Kohonen asked Jacob Gillespie commented

Is it possible to use the Server Sent Events with the Http interface in FlexSim?

Hi,

I am investigating the possibilities of using the http interface that FlexSim provides to integrate to an external control system that gives FlexSim orders about where to place items in the model. To accomplish this I need two-way communication between FlexSim and the control system. When new items arrive I need to ask the control system where to place the items, but the control system might also tell FlexSim at any time to reorder the items. The control system will most probably be located on a web server so I was looking at using the http interface.

I can implement the two-way communication by having the client to regularly poll the server for new messages, but this increases the overhead. Another option could be to use the server sent events that use the the http protocol, but I am not sure if it works with FlexSim.

Do you know if it is possible to use the server sent events with the http interface (applicationcommand("sendhttprequest")) in FlexSim? If it is has anyone used it and has some example on how it is done and what changes need to be made at the server and client end of the communication?

Also, is it possible to listen for incoming Http messages e.g. in process flow? Else, I guess polling the server is the only option.

Thank you!

Kind regards,

Axel

FlexSim 16.2.0
webserverhttpweb communicationserver sent events
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image
5 Likes"
Phil BoBo answered Phil BoBo commented

Your statement "the http interface in FlexSim" is very vague.

If you are using applicationcommand("sendhttprequest"), then FlexSim is sending synchronous http requests directly with the WinHttp API.

The other internet technologies you have mentioned (server-sent events and long-polling) assume that the client machine is rendering an HTML page and executing Javascript.

You can do that in FlexSim, but you don't use applicationcommand("sendhttprequest").

Instead, you use an HTML widget in a Custom GUI. Within the HTML widget, you can write custom HTML and Javascript code that interacts with a server on the internet.

This is how the Start Page works.

You can use the fireFlexsimEvent() function in JavaScript to execute FlexScript that affects your model. You can use the callwebscriptmethod() command in FlexScript to execute a JavaScript function in your HTML page.

From an HTML widget, you can then both interact with a server on the internet and interact with your model using whatever web technologies are supported by the server you are interacting with and the Webkit library that FlexSim uses to render HTML pages and execute Javascript. (I don't know specifically what internet technologies, such as server-sent events, long polling, ajax, etc, work with Webkit. Your best best to find out whether a certain technology works is to imeplement it within a regular webpage using a web browser such as Chrome or Firefox, then port that HTML/Javascript code into an HTML Widget in FlexSim and see if it still works.)

Webkit probably supports server-sent events, so this is probably easily doable in FlexSim by using an HTML widget (although I haven't tested it to know for sure whether this works). Here is a link to a simple example of writing client-side JavaScript and server-side php code that interacts with server-sent events.

The FlexSim Webserver is an application that performs the work of a web server (hence its name). It is used server-side (as opposed to client-side) to facilitate receiving HTTP requests, passing them on to FlexSim, receiving an HTML response from FlexSim, and then sending that HTML response over the internet back to the client, such as a web browser on someone's computer or another application that communicates via HTTP.

You could use the FlexSim Webserver to spawn an instance of FlexSim, which uses applicationcommand("sendhttprequest") to communicate with an external server, and the external server sends http requests back to the FlexSim Webserver that passes them on to FlexSim. This creates 2 synchronous communication paths where both sides are servers communicating with each other. This is probably not a very good solution for what you are trying to do; polling would be a better solution than this.

The final option is to make a custom C++ DLL that uses whatever internet technology you want to communicate, such as WebSockets. I haven't personally used it, but apparently libwebsockets is a lightweight pure C library that implements WebSockets, which provides full-duplex communication channels over a single TCP connection.

· 8
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jacob Gillespie avatar image
2 Likes"
Jacob Gillespie answered Jacob Gillespie commented

Server sent events are unfortunately not supported by the FlexSim Webserver. For now your only option is to use a polling strategy like you described.

I am currently working on a new version of the FlexSim Webserver which will be written using node.js and will support using WebSockets. This will allow us true two way communication.

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Matt Long avatar image
2 Likes"
Matt Long answered Axel Kohonen commented

The Webserver allows you to execute nodes and create custom query handlers for communicating with FlexSim. I realize it's kind of hidden inside the User Manual, so here it is pulled out from the Webserver Concepts page:

A list of default queries is available at MAIN:/project/exec/globals/serverinterface/queryhandlers. Using these as templates you can create your own custom query handlers by adding subnodes to the MODEL:/Tools/serverinterface/queryhandlers node. These queryhandler nodes should be toggled flexscript. The name of the node corresponds to a value passed in by the query. For example, webserver.dll?queryinstance=Test%20Model&instancenum=1&reset executes the queryhandler located at MAIN:/project/exec/globals/serverinterface/queryhandlers/reset. Parameters may be added to the query to pass additional information. For example, webserver.dll?queryinstance=Test%20Model&instancenum=1&customquery=1&count=5.

Custom functionality and custom query types for querying the model can also be written for a model by making a flexscript node at MODEL:/Tools/serverinterface/sendreply. This node must call webcommand("httpsendreply", replynode); once to send a reply to the query. This will send a reply in different forms, depending on the type of data in the node given as a parameter. If the node has string data containing a syntactically correct HTTP reply with HTTP headers as defined by http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html, then it will be sent as-is, giving developers full access to send any type of HTTP reply desired. Refer to the code in MAIN:/project/exec/globals/serverinterface/sendreply for an example, which can be copied and edited as needed. Remember that the defaultPage and getRunState queries are used by the job interface.

There's also a short section on how to create your own default page:

The default webpage for a model is generated by using a template found in the tree at MAIN:/project/exec/globals/serverinterface/pagetemplates/default. An html page can be made for a specific model by putting the html into a node created at MODEL:/Tools/serverinterface/defaultpagetemplate. This page can include the php-like <?flexscript ?> tags to generate dynamic material. This will override the default page of the model.

As it says, take a look at the query handlers that are implemented by FlexSim and use those as templates to create your own for passing in custom data. Hopefully this helps.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.