======================== Client - Getting Started ======================== An OPC UA client is a network client that interacts with an OPC UA server via a collection of abstract Remote Procedure Calls (RPC), which is a request/response protocol. An RPC is initiated by the client, which sends a request message to the server. The OPC UA server then sends a response to the client, and the application continues its process. While the OPC UA server is processing the call, the OPC UA client is blocked (it waits until the server has finished processing before resuming execution), unless the OPC UA client operates in asynchronous callback mode. The OPC UA client can operate in two modes, standard blocking socket call (default) or non blocking cosocket mode. Blocking sockets must run in the context of a native thread such as a thread running an LSP page. See :ref:`CosocketMode` for details. Connecting to Server -------------------- A client is created by first loading the OPC-UA stack and then creating a client as follows: .. literalinclude:: examples/client/client_create_session.lua :language: lua :lines: 1-14 `Full source <_static/client/client_create_session.lua>`__ Next, connect to the server, open an OPC-UA channel and finally create an OPC-UA session: .. literalinclude:: examples/client/client_create_session.lua :language: lua :lines: 16,20-21,28,35,48 `Full source <_static/client/client_create_session.lua>`__ You can start browsing the address space as soon as a session has been established. Browsing Address Space ---------------------- To browse the server's address space, call the 'browse' method. .. literalinclude:: examples/client/client_browse.lua :language: lua :lines: 2-11,51-53,69-71,86-110 `Full source <_static/client/client_browse.lua>`__ The :ref:`browsing` user guide has detailed overview of all parameters. Reading Node Attributes ----------------------- .. literalinclude:: examples/client/client_read.lua :language: lua :lines: 1-3,44-62 `Full source <_static/client/client_read.lua>`__ See :ref:`read_write_data` for paramerers and detailed information. Closing Server Connection ----------------------------- When finished using the server, close the server connection as follows: .. literalinclude:: examples/client/client_create_session.lua :language: lua :lines: 55,60 `Full source <_static/client/client_create_session.lua>`__ .. _CosocketMode: Asynchronous Cosocket Mode ---------------------------- Clients, by default, use blocking sockets. However, a client can be configured to operate in cosocket mode, a lightweight Lua coroutine socket thread. All client methods can accept a callback that will be called when the corresponding request completes. The OPC UA methods can also be called without providing a callback when running in cosocket mode if the caller is running in the context of another cosocket. When another cosocket calls an OPC UA client in cosocket mode without providing a callback, the calling cosocket will block. Not having to provide a callback when operating in cosocket mode makes designing OPC UA clients easier. We recommend reading the Barracuda App Server's `Socket Design User Guide <../doc/?url=SockLib.html>`_ for an introduction to cosockets and for details on the various socket modes. The cosocket mode is set in the configuration table with the flag 'cosocketMode'. .. literalinclude:: examples/client/client_cosock_callback.lua :language: lua :lines: 1-13,17 `Full source <_static/client/client_cosock_callback.lua>`__ All callbacks, except for the connect method must accept two arguments. The connect method receives one argument as shown in the two examples below. .. literalinclude:: examples/client/client_cosock_callback.lua :language: lua :lines: 45,46 `Full source <_static/client/client_cosock_callback.lua>`__ The connect method's error argument is nil if the connection succeeded. .. literalinclude:: examples/client/client_cosock_callback.lua :language: lua :lines: 32-41 `Full source <_static/client/client_cosock_callback.lua>`__ All other callbacks receive two arguments, the response and an error code. This corresponds to the two return values returned when operating in blocking mode (when not providing a callback). The methods return response,nil on success and nil,error on failure. .. literalinclude:: examples/client/client_cosock_callback.lua :language: lua :lines: 19,22-30 `Full source <_static/client/client_cosock_callback.lua>`_