Method: Mosquitto::Client#loop_forever

Defined in:
ext/mosquitto/client.c

#loop_forever(10, 1) ⇒ Boolean

This function calls Mosquitto::Client#loop for you in an infinite blocking loop. It is useful for the case where you only want to run the MQTT client loop in your program.

It handles reconnecting in case server connection is lost. If you call Mosquitto::Client#disconnect in a callback it will return.

Examples:

client.loop_forever(10, 1)

Returns:

  • (Boolean)

Parameters:

  • timeout (Integer)

    Maximum number of milliseconds to wait for network activity in the select() call before timing out. Set to 0 for instant return. Set negative to use the default of 1000ms

  • max_packets (Integer)

    this parameter is currently unused and should be set to 1 for future compatibility.

Returns:

  • (true)

    on success

Raises:

  • (Mosquitto::Error, SystemCallError)

    on invalid input params or system call errors



1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
# File 'ext/mosquitto/client.c', line 1492

static VALUE rb_mosquitto_client_loop_forever(VALUE obj, VALUE timeout, VALUE max_packets)
{
    struct nogvl_loop_args args;
    int ret;
    MosquittoGetClient(obj);
    Check_Type(timeout, T_FIXNUM);
    Check_Type(max_packets, T_FIXNUM);
    args.mosq = client->mosq;
    args.timeout = NUM2INT(timeout);
    args.max_packets = NUM2INT(max_packets);
    ret = (int)rb_thread_call_without_gvl(rb_mosquitto_client_loop_forever_nogvl, (void *)&args, rb_mosquitto_client_loop_forever_ubf, client);
    switch (ret) {
       case MOSQ_ERR_INVAL:
           MosquittoError("invalid input params");
           break;
       case MOSQ_ERR_NOMEM:
           rb_memerror();
           break;
       case MOSQ_ERR_NO_CONN:
           MosquittoError("client not connected to broker");
           break;
       case MOSQ_ERR_CONN_LOST:
           MosquittoError("connection to the broker was lost");
           break;
       case MOSQ_ERR_PROTOCOL:
           MosquittoError("protocol error communicating with the broker");
           break;
       case MOSQ_ERR_ERRNO:
           rb_sys_fail("mosquitto_loop");
           break;
       default:
           return Qtrue;
    }
}