Fork me on GitHub

Low level network API

C API Reference mgos_mongoose.h

#include "mgos_mongoose.h"

struct mg_mgr *mgos_get_mgr(void);

Return global event manager

enum mgos_init_result mongoose_init(void);

int mongoose_poll(int ms);

void mongoose_destroy(void);

typedef void (*mgos_poll_cb_t)(void *cb_arg);

void mgos_add_poll_cb(mgos_poll_cb_t cb, void *cb_arg);

void mgos_remove_poll_cb(mgos_poll_cb_t cb, void *cb_arg);

void mgos_wdt_set_feed_on_poll(bool enable);

void mgos_set_enable_min_heap_free_reporting(bool enable);

char *mgos_get_nameserver(void);

void mongoose_schedule_poll(bool from_isr);

Schedule MG poll ASAP. Note: may be called from ISR context.

struct mg_connection *mgos_bind(const char *addr, mg_event_handler_t func,
                                void *ud);

struct mg_connection *mgos_connect(const char *addr, mg_event_handler_t func,
                                   void *ud);

struct mg_connection *mgos_connect_ssl(const char *addr, mg_event_handler_t f,
                                       void *ud, const char *cert,
                                       const char *key, const char *ca_cert);

void mgos_disconnect(struct mg_connection *c);

struct mg_connection *mgos_bind_http(const char *addr);

bool mgos_add_http_endpoint(struct mg_connection *c, const char *uri,
                            mg_event_handler_t handler, void *user_data);

struct mg_connection *mgos_connect_http(const char *addr, mg_event_handler_t,
                                        void *ud);

struct mg_connection *mgos_connect_http_ssl(const char *addr,
                                            mg_event_handler_t f, void *ud,
                                            const char *cert, const char *key,
                                            const char *ca_cert);

JAVASCRIPT API Reference api_net.js


Net.ctos(conn, local, ip, port)

Convert address of a connection conn to string. Set local to true to stringify local address, otherwise false to stringify remote. Set ip to true to stringify IP, port to stringify port. Example:

print('Connection from:', Net.ctos(conn, false, true, true));

Net.discard(conn, len)

Remove initial len bytes of data from the connection's conn receive buffer in order to discard that data and reclaim RAM to the system.


Start TCP or UDP server. options is an object:

   // Required. Port to listen on, 'tcp://PORT' or udp://PORT.
   addr: 'tcp://1234',
   // Optional. Called when connection is established.
   onconnect: function(conn) {}, 
   // Optional. Called when new data is arrived.
   ondata: function(conn, data) {},
   // Optional. Called when protocol-specific event is triggered.
   onevent: function(conn, data, ev, edata) {},
   // Optional. Called when the connection is about to close.
   onclose: function(conn) {},
   // Optional. Called when on connection error.
   onerror: function(conn) {},

Example - a UDP echo server. Change udp:// to tcp:// to turn this example into the TCP echo server:

  addr: 'udp://1234',
  ondata: function(conn, data) {
    print('Received from:', Net.ctos(conn, false, true, true), ':', data);
    Net.send(conn, data);            // Echo received data back
    Net.discard(conn, data.length);  // Discard received data


Connect to a remote host. options is the same as for the Net.serve. The addr format is [PROTO://]HOST:PORT. PROTO could be tcp or udp. HOST could be an IP address or a host name. If HOST is a name, it will be resolved asynchronously.

Examples of valid addresses:, udp://,, [::1]:80.


Send all pending data to the remote peer, and disconnect when all data is sent. Return value: none.

Net.send(conn, data)

Send data to the remote peer. data is an mJS string. Return value: none.

Net.setStatusEventHandler(handler, data)

Set network status handler. A handler is a function that receives events: Net.STATUS_DISCONNECTED, Net.STATUS_CONNECTING, Net.STATUS_CONNECTED, Net.STATUS_GOT_IP.