OnConnectState¶
Terminal connection-state events โ know when a terminal goes up, down or reconnects.
API Information:
- Low-level API:
MT4Account.OnConnectState(ctx, terminalIds)(defined inmt4/MT4Account.go) - gRPC service:
mt4_term_api.Connection - RPC:
OnConnectState(OnConnectStateRequest) โ stream OnConnectStateReply - Payload:
ConnectStateData
๐ฌ Just the essentials¶
- What it is. Health/heartbeat stream for the MT4 terminal(s) behind your session.
- Why you need it. Surface disconnects to the user; gate trading on a live link.
- When to use. Dashboards, watchdogs, "is the bridge alive?" checks.
๐ฏ Purpose¶
- Watch terminal connection state in real time
- Detect disconnects and read the reason
- Gate trading on a confirmed-live link
- Watch specific terminals or just your own
Method Signature¶
func (a *MT4Account) OnConnectState(ctx context.Context, terminalIds []string) (<-chan *pb.ConnectStateData, <-chan error)
Request message:
message OnConnectStateRequest { repeated string terminal_ids = 1; }
Payload message:
message ConnectStateData {
string state = ...;
bool is_connected = ...;
bool is_alive = ...; bool api_is_alive = ...;
string server = ...;
uint64 account = ...;
string error_message = ...;
Timestamp timestamp = ...;
}
๐ฝ Input¶
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx |
context.Context |
yes | Cancel it to unsubscribe and close the channels. |
terminalIds |
[]string |
no | Specific terminals to watch; nil = this account's own terminal. |
โฌ๏ธ Frame โ *pb.ConnectStateData¶
| Field | Proto Type | Go getter | Description |
|---|---|---|---|
state |
string | GetState() string |
Human-readable state |
is_connected |
bool | GetIsConnected() bool |
Link up to the broker |
is_alive / api_is_alive |
bool | GetIsAlive() / GetApiIsAlive() |
Terminal / API process alive |
server |
string | GetServer() string |
Broker server name |
account |
uint64 | GetAccount() uint64 |
Login |
error_message |
string | GetErrorMessage() string |
Reason, when down |
timestamp |
Timestamp | GetTimestamp() |
Event time |
A non-nil value on the error channel ends the stream; both channels close on
ctxcancel.
๐งฉ Notes & Tips¶
- Lives on
ConnectionClientโ notSubscriptionClientlike the otherOn*streams. nilterminalIds = just watch your own terminal (the common case).- Check
GetErrorMessage()whenGetIsConnected()is false for the reason.
๐ Usage Examples¶
1) Watch the terminal's state¶
func watchState(ctx context.Context, acc *mt4.MT4Account) error {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
data, errs := acc.OnConnectState(ctx, nil)
for {
select {
case d := <-data:
fmt.Printf("%s connected=%v alive=%v\n",
d.GetState(), d.GetIsConnected(), d.GetIsAlive())
case err := <-errs:
if err != nil { return err }
case <-ctx.Done():
return nil
}
}
}
2) Flag disconnects with the reason¶
func flagDisconnects(ctx context.Context, acc *mt4.MT4Account) error {
data, errs := acc.OnConnectState(ctx, nil)
for {
select {
case d := <-data:
if !d.GetIsConnected() {
fmt.Printf("โ ๏ธ %s disconnected: %s\n", d.GetServer(), d.GetErrorMessage())
}
case err := <-errs:
if err != nil { return err }
case <-ctx.Done():
return nil
}
}
}
3) Gate: only trade while connected¶
func waitConnected(ctx context.Context, acc *mt4.MT4Account) (bool, error) {
data, errs := acc.OnConnectState(ctx, nil)
for {
select {
case d := <-data:
if d.GetIsConnected() && d.GetApiIsAlive() { return true, nil }
case err := <-errs:
if err != nil { return false, err }
case <-ctx.Done():
return false, nil
}
}
}
โ ๏ธ Pitfalls¶
- Different client. Needs a live
ConnectionClientโ it won't stream before connect. - Watchdog, not a trigger โ use it to gate, but also handle errors on the actual calls.
- Leaking the context leaks the goroutine โ
defer cancel().
๐งช Testing Suggestions¶
- Happy path (demo). On a healthy session,
GetIsConnected()is true andGetState()is a normal value. - Reason. When down,
GetErrorMessage()is populated. - Scoped. Passing a specific terminal id streams just that terminal.