#
iot
IOT is a communication system to the internet.
You can use this to create secure connections to external backends you create.
Concerns of Usage
These functions are not made for performing actions breaking IoT ethics.
Please use these functions simply for I/O & communication.
#
Enums
iot.method: {[index: string]: string}
- Contains a list of method procedures for HTTP/HTTPS
iot.status: {[index: string | number]: string | number}
- Contains a list of status codes for HTTP/HTTPS
iot.opcode: {[index: string | number]: string | number}
- Contains a list of opcode procedures for sockets
iot.closure: {[index: string | number]: string | number}
- Contains a list of closure procedures for sockets
- These are usually needed when dealing with
iot.closure.close
cases
#
Functions
iot.http(url: string, callback?: function(response: http_response), options?: http_options)
- Makes an HTTP/HTTPS request to an address or domain.
- This function is asynchronous, meaning this will not "freeze" Lua.
- The interface types
http_options
&http_response
can be seen here:
body?: string
method?: string
headers?: {[index: string]: string}
parameters?: {[index: string]: string}
progress?: function(dl_total: number, dl_now: number, up_total: number, up_now: number): boolean?
headers: {[index: string]: string}
body: string
status: number
reason: string
url: string
iot.stream(url: string, callback?: function(response: http_response), options?: http_options)
- Similar to
iot.http
except allows multiple returns from segments of encoded chunks. - When receiving chunks, all responses will have a
100
status code for continuing. - Once finished, the status code will either be
200
or a different value if it errored. - Returning
false
inside the callback will cancel the stream.
iot.socket(url: string, headers?: {[index: string]: string}): socket_class
- Creates a socket object which is connected to an address or domain.
- Upon creation, the socket isn't activated until you invoke it to do so.
- Sockets work by reference, so if there is no-longer a use in lua, it will destroy itself.
iot.serve(port: number): serve_class
- Creates an HTTP server for requests, similar to express/deno.
- Do note that the server doesn't actually start until you invoke
serve:start()
- Unlike client sockets, if the object gets GC'ed, it won't cleanup the callbacks or invoke a destructor.
- If you "lose" the handle object, you can retrieve it back by simply calling the function again and its exact port number.
#
Serve
These are interface & function definitions for how we handle serve_class
serve:start(): boolean
- Attempts to allocate a port for HTTP connections.
- If this fails it will return
false
. - Usually a failure indicates a port is already in-use.
serve:stop()
- De-allocates and stops serving under the allocated port.
- This doesn't actually destroy the object.
serve:active(): boolean
- Checks if the current serve is active on its port.
serve:port(): number
- Gets the current port the serve is on.
serve:handlers(): {method: string, path: string, callback: function}[]
- Gets all handlers associated with the serve.
serve:sockets(path: string): serve_socket[]
- Gets all sockets associated with the serve.
serve:upgrade(): serve_socket
- Upgrades a connection into a socket.
- Must be used inside of a HTTP request.
serve:socket(path: string, callback: function(connection: serve_socket, opcode: number, data: string | number, extra: string))
- Adds a callback for a certain path for socket-based messages.
- Do note that the path must be the same to the socket you upgraded in.
- Opcodes can be seen under
iot.opcode
serve:any(path: string, callback: function(req: { method: string, path: string, query: string, body: string, headers: table}))
- Any is a form of method that allows all types of methods to be seen in callback under the path.
- You can check the method by seeing
req.method
- If you set the path to
any
it will also act as a any filter, allowing you to process paths individually. - Do note this is first-order making it called first before any other methods
serve:get(path: string, callback: function(req: { method: string, path: string, query: string, body: string, headers: table}))
- Adds a callback for a certain path under "GET" requests
- The GET method requests a representation of the specified resource.
- Requests using GET should only retrieve data and should not contain a request content.
serve:post(path: string, callback: function(req: { method: string, path: string, query: string, body: string, headers: table}))
- Adds a callback for a certain path under "POST" requests
- The HEAD method asks for a response identical to a GET request, but without a response body.
serve:head(path: string, callback: function(req: { method: string, path: string, query: string, body: string, headers: table}))
- Adds a callback for a certain path under "HEAD" requests
- The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server.
serve:put(path: string, callback: function(req: { method: string, path: string, query: string, body: string, headers: table}))
- Adds a callback for a certain path under "PUT" requests
- The PUT method replaces all current representations of the target resource with the request content.
serve:delete(path: string, callback: function(req: { method: string, path: string, query: string, body: string, headers: table}))
- Adds a callback for a certain path under "DELETE" requests
- The DELETE method deletes the specified resource.
serve:options(path: string, callback: function(req: { method: string, path: string, query: string, body: string, headers: table}))
- Adds a callback for a certain path under "OPTIONS" requests
- The OPTIONS method describes the communication options for the target resource.
#
Serve Socket
These are interface & function definitions for how we handle serve_socket
serve_socket:id(): string
- Gets the socket's unique ID.
- Useful if you need to manually track them.
serve_socket:disconnect(code?: string | number, reason?: string)
- Forces a socket to disconnect.
- By providing a string in the code parameter, it will become a reason with normal closure.
serve_socket:text(payload: string)
- Sends a text-based payload.
serve_socket:binary(payload: string)
- Sends a binary-based payload.
serve_socket:ping(payload?: string)
- Sends a ping payload, typically used to keep connections alive.
serve_socket:pong(payload?: string)
- Sends a pong payload, typically used to keep connections alive.
#
Socket
These are interface & function definitions for how we handle socket_class
#
Functions
socket:connect()
- Attempt to connect the socket to the constructed URL from creation.
socket:disconnect(reason?: string)
- Disconnects from a socket server with a message if needed.
socket:text(payload: string)
- Send a payload message to the socket server.
socket:utf8(payload: string)
- Send a payload message to the socket server in UTF8 format.
socket:binary(payload: string)
- Send a payload message to the socket server in binary format.
socket:close(reason?: string)
- Closes the socket connection, this usually also sends a message.
- If you want to re-open the connection, simply run
socket.connect()
socket:ping(message?: string)
- Sends a ping payload to the socket server.
socket:get_url(): string
- Gets the current full connection URL.
socket:get_host(): string
- Gets the current host from the URL.
socket:get_port(): string
- Gets the current port from the URL.
socket:get_path(): string
- Gets the current path from the URL.
socket:headers_add(key: string, value: string)
- Adds a header to the list of headers to be sent on connection.
socket:headers_remove(key: string)
- Removes a header from the list of headers to be sent on connection.
socket:headers_get(key: string): string?
- Gets a header by the key-name.
socket:headers_all(key: string): {[index: string]: string}
- Gets a list of all headers applied.
socket:is_open(): boolean
- If the socket is currently connected.
socket:is_connecting(): boolean
- If the socket is currently connecting.
socket:is_closing(): boolean
- If the socket is currently closing.
socket:is_closed(): boolean
- If the socket is currently closed.
socket:is_tls(): boolean
- If the socket is using TLS as it's connection layer.
#
Events
This internally uses the Signal library for C++ communication.
These can be accessed for example by: socket_class.add(...)
open()
- Called when the socket engine has made a successful connection.
error(code: number, reason: string)
- Called if there was a connection failure.
close(status: number, reason: string)
- Called when the connection closes, this can contain an error reason.
message(payload: string, is_binary: boolean)
- Called when a message has been received.
ping(message: string)
- Called when the socket server sent a ping.
- This can be used as an "isalive" status refresh.
- However this is managed internally by interstellar.
pong(message: string)
- Called when the socket server sent a pong.
- This can be used as an "isalive" status refresh.