The following snippet of code was posted by a member of the emqtt team.
here is the minimal stuff you want, assuming you are connecting to "127.0.0.1:14567"
{ok, Pid} = emqtt:start_link([{host, {127,0,0,1}},
{port, 14567}
]),
_ =emqtt:quic_connect(Pid),
Here is my code. It is using the same minimal emqtt function above. The only thing difference is the host IP address. I am running the code on Debain 12 hosting Erlang/OTP 27.
-module(com2).
-behaviour(gen_statem).
-define(NAME, com2).
-export([start/0, stop/0]).
-export([cnct/0, sub/0]).
-export([cnctState/3, subState/3]).
-export([init/1,callback_mode/0, handle_event/3, terminate/3, code_change/4]).
start() ->
{ok, Pid} = gen_statem:start_link({local,?NAME}, ?MODULE, [], []),
io:format("My proccess Pid ~p~n", [Pid]).
cnct() ->
gen_statem:cast(?NAME, cnct).
sub() ->
%gen_statem:call(?NAME, sub).
gen_statem:cast(?NAME, sub).
stop() ->
gen_statem:stop(?NAME).
-record(comm, {connPid, emqttMsg}).
callback_mode() ->
[state_functions,state_enter].
init(_Args) ->
process_flag(trap_exit, true),
Properties = #{'Session-Expiry-Interval' => 300,
'Server-Keep-Alive' => 30,
'Retain-Available' => 1
},
Options = [ {host, "192.168.178.33"},
{port, 14567}
%%{keepalive, 30},
%{clientid, <<"alb65McbrideBCCanada">>},
%%{proto_ver, v5},
%%{reconnect_timeout, 1000}
%{quic_opts, {quicer:conn_opts(), quicer:stream_opts()}},
%%{properties, Properties}
],
{ok, ConnPid} = emqtt:start_link(Options),
io:format("ConnPid from emqtt:start_link/1 ~p~n", [ConnPid]),
State = cnctState, Data = #comm{connPid = ConnPid},
%io:format("Data ~p~n", [Data]),
{ok, State, Data}.
cnctState(cast, cnct, Data) ->
ConnPid = Data#comm.connPid,
io:format("ConnPid for emqtt:quic_connect/1 ~p~n", [ConnPid]),
ConnPid = Data#comm.connPid,
emqtt:quic_connect(ConnPid),
%emqtt:open_quic_connection(ConnPid),
%emqtt:quic_mqtt_connect(ConnPid),
{next_state, subState, Data};
cnctState(EventType, EventContent, Data) ->
handle_event(EventType, EventContent, Data).
%subState({call, From}, sub, Data) ->
subState(cast, sub, Data) ->
ConnPid = Data#comm.connPid,
io:format("CommPid for emqtt:subscribe/3 ~p~n", [ConnPid]),
%Properties = #{},
Properties = #{ %'Maximum-Packet-Size' => 1048576,
%'Receive-Maximum' => 32,
%'Retain-Available' => 1,
%'Shared-Subscription-Available' => 1,
%'Subscription-Identifier-Available' => 1,
%'Topic-Alias-Maximum' => 65535,
%'Wildcard-Subscription-Available' => 1,
%%'Message-Expiry-Interval' => 10,
%'Content-Type' => <<"Json">>,
%%'Payload-Format-Indicator' => 1,
%%'Content-Type' => <<"application/json">>,
%%'Server-Keep-Alive' => 30
%'Maximum-QoS' => 1
},
SubOpts = [qos1],
Subscriptions = [{<<"EK9160/EK-98EFB6/Stream1/Json/Tx/Data">>, SubOpts}],
_ = emqtt:subscribe(ConnPid, Properties, Subscriptions),
%io:format("Subscription~p~n", [Reply]),
%{keep_state,Data, [{noreply, From, Data}]};
{next_state, subState, Data};
subState(EventType, EventContent, Data) ->
handle_event(EventType, EventContent, Data).
handle_event(_, _, Data) ->
%% Ignore all other events
{keep_state, Data}.
terminate(_Reason, _State, _Data) ->
ok.
code_change(_Vsn, State, Data, _Extra) →
{ok, State, Data}.
When I run the com2.erl module shown above, I get the following result
1> com2:start().
ConnPid from emqtt:start_link/1 <0.209.0>
My proccess Pid <0.208.0>
ok
2> com2:cnct().
ConnPid for emqtt:quic_connect/1 <0.209.0>
ok
3> com2:sub().
CommPid for emqtt:subscribe/3 <0.209.0>
ok
4>
With emqtt it is possible to subscribe and publish through the command line/Linux Shell but we not been able to create Erlang/OTP clients using emqtt. Could someone help us with this issue.