Socket Runtime provides a minimalist DSL that makes it easy to create
cross platform native system and context menus.
Menus are created at run time. They can be created from either the Main or
Render process. The can be recreated instantly by calling the setSystemMenu method.
The method takes a string. Here's an example of a menu. The semi colon is
significant indicates the end of the menu. Use an underscore when there is no
accelerator key. Modifiers are optional. And well known OS menu options like
the edit menu will automatically get accelerators you dont need to specify them.
socket.application.setSystemMenu({ index: 0, value: `
App:
Foo: f;
Edit:
Cut: x
Copy: c
Paste: v
Delete: _
Select All: a;
Other:
Apple: _
Another Test: T
!Im Disabled: I
Some Thing: S + Meta
---
Bazz: s + Meta, Control, Alt;
`)
Separators
To create a separator, use three dashes ---.
Accelerator Modifiers
Accelerator modifiers are used as visual indicators but don't have a
material impact as the actual key binding is done in the event listener.
A capital letter implies that the accelerator is modified by the Shift key.
Additional accelerators are Meta, Control, Option, each separated
by commas. If one is not applicable for a platform, it will just be ignored.
On MacOS Meta is the same as Command.
Disabled Items
If you want to disable a menu item just prefix the item with the ! character.
This will cause the item to appear disabled when the system menu renders.
Submenus
We feel like nested menus are an anti-pattern. We don't use them. If you have a
strong argument for them and a very simple pull request that makes them work we
may consider them.
Event Handling
When a menu item is activated, it raises the menuItemSelected event in
the front end code, you can then communicate with your backend code if you
want from there.
For example, if the Apple item is selected from the Other menu...
Start scanning for published values that correspond to a well-known UUID.
Once subscribed to a UUID, events that correspond to that UUID will be
emitted. To receive these events you can add an event listener, for example...
const ble = new Bluetooth(id)
ble.subscribe(uuid)
ble.on(uuid, (data, details) => {
// ...do something interesting
})
This module enables name resolution. For example, use it to look up IP
addresses of host names. Although named for the Domain Name System (DNS),
it does not always use the DNS protocol for lookups. dns.lookup() uses the
operating system facilities to perform name resolution. It may not need to
perform any network communication. To perform name resolution the way other
applications on the same system do, use dns.lookup().
External docs: https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback
Resolves a host name (e.g. example.org) into the first found A (IPv4) or
AAAA (IPv6) record. All option properties are optional. If options is an
integer, then it must be 4 or 6 – if options is 0 or not provided, then IPv4
and IPv6 addresses are both returned if found.
From the node.js website...
With the all option set to true, the arguments for callback change to (err,
addresses), with addresses being an array of objects with the properties
address and family.
On error, err is an Error object, where err.code is the error code. Keep in
mind that err.code will be set to 'ENOTFOUND' not only when the host name does
not exist but also when the lookup fails in other ways such as no available
file descriptors. dns.lookup() does not necessarily have anything to do with
the DNS protocol. The implementation uses an operating system facility that
can associate names with addresses and vice versa. This implementation can
have subtle but important consequences on the behavior of any Node.js program.
Please take some time to consult the Implementation considerations section
before using dns.lookup().
Argument
Type
Default
Optional
Description
hostname
string
false
The host name to resolve.
options
object | intenumberger
true
An options object or record family.
options.family
number | string
0
true
The record family. Must be 4, 6, or 0. For backward compatibility reasons,'IPv4' and 'IPv6' are interpreted as 4 and 6 respectively. The value 0 indicates that IPv4 and IPv6 addresses are both returned. Default: 0.
cb
function
false
The function to call after the method is complete.
This module enables name resolution. For example, use it to look up IP
addresses of host names. Although named for the Domain Name System (DNS),
it does not always use the DNS protocol for lookups. dns.lookup() uses the
operating system facilities to perform name resolution. It may not need to
perform any network communication. To perform name resolution the way other
applications on the same system do, use dns.lookup().
The record family. Must be 4, 6, or 0. For backward compatibility reasons,'IPv4' and 'IPv6' are interpreted as 4 and 6 respectively. The value 0 indicates that IPv4 and IPv6 addresses are both returned. Default: 0.
External docs: https://nodejs.org/api/dgram.html#socketbindport-address-callback
Listen for datagram messages on a named port and optional address
If the address is not specified, the operating system will attempt to
listen on all addresses. Once the binding is complete, a 'listening'
event is emitted and the optional callback function is called.
If binding fails, an 'error' event is emitted.
Argument
Type
Default
Optional
Description
port
number
false
The port to listen for messages on
address
string
false
The address to bind to (0.0.0.0)
callback
function
false
With no parameters. Called when binding is complete.
External docs: https://nodejs.org/api/dgram.html#socketconnectport-address-callback
Associates the dgram.Socket to a remote address and port. Every message sent
by this handle is automatically sent to that destination. Also, the socket
will only receive messages from that remote peer. Trying to call connect()
on an already connected socket will result in an ERR_SOCKET_DGRAM_IS_CONNECTED
exception. If the address is not provided, '0.0.0.0' (for udp4 sockets) or '::1'
(for udp6 sockets) will be used by default. Once the connection is complete,
a 'connect' event is emitted and the optional callback function is called.
In case of failure, the callback is called or, failing this, an 'error' event
is emitted.
Argument
Type
Default
Optional
Description
port
number
false
Port the client should connect to.
host
string
true
Host the client should connect to.
connectListener
function
true
Common parameter of socket.connect() methods. Will be added as a listener for the 'connect' event once.
External docs: https://nodejs.org/api/dgram.html#socketdisconnect
A synchronous function that disassociates a connected dgram.Socket from
its remote address. Trying to call disconnect() on an unbound or already
disconnected socket will result in an ERR_SOCKET_DGRAM_NOT_CONNECTED exception.
External docs: https://nodejs.org/api/dgram.html#socketsendmsg-offset-length-port-address-callback
Broadcasts a datagram on the socket. For connectionless sockets, the
destination port and address must be specified. Connected sockets, on the
other hand, will use their associated remote endpoint, so the port and
address arguments must not be set.
The msg argument contains the message to be sent. Depending on its type,
different behavior can apply. If msg is a Buffer, any TypedArray, or a
DataView, the offset and length specify the offset within the Buffer where
the message begins and the number of bytes in the message, respectively.
If msg is a String, then it is automatically converted to a Buffer with
'utf8' encoding. With messages that contain multi-byte characters, offset,
and length will be calculated with respect to byte length and not the
character position. If msg is an array, offset and length must not be
specified.
The address argument is a string. If the value of the address is a hostname,
DNS will be used to resolve the address of the host. If the address is not
provided or otherwise nullish, '0.0.0.0' (for udp4 sockets) or '::1'
(for udp6 sockets) will be used by default.
If the socket has not been previously bound with a call to bind, the socket
is assigned a random port number and is bound to the "all interfaces"
address ('0.0.0.0' for udp4 sockets, '::1' for udp6 sockets.)
An optional callback function may be specified as a way of reporting DNS
errors or for determining when it is safe to reuse the buf object. DNS
lookups delay the time to send for at least one tick of the Node.js event
loop.
The only way to know for sure that the datagram has been sent is by using a
callback. If an error occurs and a callback is given, the error will be
passed as the first argument to the callback. If a callback is not given,
the error is emitted as an 'error' event on the socket object.
Offset and length are optional but both must be set if either is used.
They are supported only when the first argument is a Buffer, a TypedArray,
or a DataView.
External docs: https://nodejs.org/api/dgram.html#socketaddress
Returns an object containing the address information for a socket. For
UDP sockets, this object will contain address, family, and port properties.
This method throws EBADF if called on an unbound socket.
External docs: https://nodejs.org/api/dgram.html#socketremoteaddress
Returns an object containing the address, family, and port of the remote
endpoint. This method throws an ERR_SOCKET_DGRAM_NOT_CONNECTED exception
if the socket is not connected.
This module enables interacting with the file system in a way modeled on
standard POSIX functions.
The Application Sandbox restricts access to the file system.
iOS Application Sandboxing has a set of rules that limits access to the file
system. Apps can only access files in their own sandboxed home directory.
Directory
Description
Documents
The app’s sandboxed documents directory. The contents of this directory are backed up by iTunes and may be set as accessible to the user via iTunes when UIFileSharingEnabled is set to true in the application's info.plist.
Library
The app’s sandboxed library directory. The contents of this directory are synchronized via iTunes (except the Library/Caches subdirectory, see below), but never exposed to the user.
Library/Caches
The app’s sandboxed caches directory. The contents of this directory are not synchronized via iTunes and may be deleted by the system at any time. It's a good place to store data which provides a good offline-first experience for the user.
Library/Preferences
The app’s sandboxed preferences directory. The contents of this directory are synchronized via iTunes. Its purpose is to be used by the Settings app. Avoid creating your own files in this directory.
tmp
The app’s sandboxed temporary directory. The contents of this directory are not synchronized via iTunes and may be deleted by the system at any time. Although, it's recommended that you delete data that is not necessary anymore manually to minimize the space your app takes up on the file system. Use this directory to store data that is only useful during the app runtime.
Specifies where to begin reading from in the file. If position is null or -1 , data will be read from the current file position, and the file position will be updated. If position is an integer, the file position will be unchanged.
This module enables interacting with the file system in a way modeled on
standard POSIX functions.
The Application Sandbox restricts access to the file system.
iOS Application Sandboxing has a set of rules that limits access to the file
system. Apps can only access files in their own sandboxed home directory.
Directory
Description
Documents
The app’s sandboxed documents directory. The contents of this directory are backed up by iTunes and may be set as accessible to the user via iTunes when UIFileSharingEnabled is set to true in the application's info.plist.
Library
The app’s sandboxed library directory. The contents of this directory are synchronized via iTunes (except the Library/Caches subdirectory, see below), but never exposed to the user.
Library/Caches
The app’s sandboxed caches directory. The contents of this directory are not synchronized via iTunes and may be deleted by the system at any time. It's a good place to store data which provides a good offline-first experience for the user.
Library/Preferences
The app’s sandboxed preferences directory. The contents of this directory are synchronized via iTunes. Its purpose is to be used by the Settings app. Avoid creating your own files in this directory.
tmp
The app’s sandboxed temporary directory. The contents of this directory are not synchronized via iTunes and may be deleted by the system at any time. Although, it's recommended that you delete data that is not necessary anymore manually to minimize the space your app takes up on the file system. Use this directory to store data that is only useful during the app runtime.
The optional options argument can be an integer specifying mode (permission and sticky bits), or an object with a mode property and a recursive property indicating whether parent directories should be created. Calling fs.mkdir() when path is a directory that exists results in an error only when recursive is false.
options.recursive
boolean
false
true
Recursively create missing path segments.
options.mode
number
0o777
true
Set the mode of directory, or missing path segments when recursive is true.
Return Value
Type
Description
Not specified
Promise
Upon success, fulfills with undefined if recursive is false, or the first directory path created if recursive is true.
This is a low-level API that you don't need unless you are implementing
a library on top of Socket SDK. A Socket SDK app has two or three processes.
When you need to send a message to another window or to the backend, you
should use the application module to get a reference to the window and
use the send method to send a message.
The Render process, is the UI where the HTML, CSS, and JS are run.
The Bridge process, is the thin layer of code that manages everything.
The Main process, is for apps that need to run heavier compute jobs. And
unlike electron it's optional.
The Bridge process manages the Render and Main process, it may also broker
data between them.
The Binding process uses standard input and output as a way to communicate.
Data written to the write-end of the pipe is buffered by the OS until it is
read from the read-end of the pipe.
The IPC protocol uses a simple URI-like scheme. Data is passed as
ArrayBuffers.
External docs: https://nodejs.org/api/os.html#os_os_networkinterfaces
Returns an object containing network interfaces that have been assigned a network address.
Each key on the returned object identifies a network interface. The associated value is an array of objects that each describe an assigned network address.
The properties available on the assigned network address object include:
address <string> - The assigned IPv4 or IPv6 address.
netmask <string> - The IPv4 or IPv6 network mask.
family <string> - The address family ('IPv4' or 'IPv6').
mac <string> - The MAC address of the network interface.
internal <boolean> - Indicates whether the network interface is a loopback interface.
scopeid <number> - The numeric scope ID (only specified when family is 'IPv6').
cidr <string> - The CIDR notation of the interface.
Return Value
Type
Description
Not specified
object
An object containing network interfaces that have been assigned a network address.
Don't use this module directly, get instances of ApplicationWindow with
socket:application methods like getCurrentWindow, createWindow,
getWindow, and getWindows.