https://github.com/ulwanski/unix-dgram-socket
Node implementation of unix datagram socket writed in TypeScript
https://github.com/ulwanski/unix-dgram-socket
dgram ipc node nodejs nodejs-modules nodejs-socket socket unix-socket
Last synced: 3 days ago
JSON representation
Node implementation of unix datagram socket writed in TypeScript
- Host: GitHub
- URL: https://github.com/ulwanski/unix-dgram-socket
- Owner: ulwanski
- License: gpl-3.0
- Created: 2019-02-13T22:20:49.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-04T13:13:16.000Z (almost 3 years ago)
- Last Synced: 2025-10-12T09:45:28.352Z (3 months ago)
- Topics: dgram, ipc, node, nodejs, nodejs-modules, nodejs-socket, socket, unix-socket
- Language: TypeScript
- Homepage:
- Size: 1.83 MB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# unix-dgram-socket
[](https://github.com/ulwanski/unix-dgram-socket/actions/workflows/compatibility.yml)
Connection-less, reliable unix datagram socket implementation with abstract namespace support for local interprocess communication in Node.JS application.
UNIX domain sockets can be either unnamed, or bound to a filesystem pathname (marked as being of type socket).
Linux also supports an abstract namespace which is independent of the filesystem.
[](https://codeclimate.com/github/ulwanski/unix-dgram-socket/maintainability)
[](https://codeclimate.com/github/ulwanski/unix-dgram-socket/test_coverage)
[](https://www.codacy.com/gh/ulwanski/unix-dgram-socket/dashboard?utm_source=github.com&utm_medium=referral&utm_content=ulwanski/unix-dgram-socket&utm_campaign=Badge_Grade)


## Installation
```bash
npm i unix-dgram-socket --save
```
Package C++ addons will be compiled during installation.
## Examples
#### Open socket
```typescript
import {UnixDgramSocket} from "unix-dgram-socket";
const socket = new UnixDgramSocket();
// Call on error
socket.on('error', (error: any) => {
console.log(error);
});
// Call when new message is received
socket.on('message', (message: Buffer, info: any) => {
console.log(message.toString(UnixDgramSocket.payloadEncoding));
console.log(info);
});
// Call when socket is bind to path
socket.on('listening', (path: string) => {
console.log(`socket listening on path: ${path}`);
});
// Bind socket to filesystem path
socket.bind("/tmp/socket1.sock");
```
#### Sending messages
```typescript
import {UnixDgramSocket} from "unix-dgram-socket";
const socket = new UnixDgramSocket();
// Call on error
socket.on('error', (error: any) => {
console.log(error);
});
// Call when new message is received
socket.on('message', (message: Buffer, info: any) => {
console.log(message.toString(UnixDgramSocket.payloadEncoding));
console.log(info);
});
// Call on successful connect
socket.on('connect', (path: string) => {
console.log(`socket connected to path: ${path}`);
});
// Call when socket is bind to path
socket.on('listening', (path: string) => {
console.log(`socket listening on path: ${path}`);
});
socket.send("Special inter-process delivery!", "/tmp/socket1.sock");
// Dgram socket is connection-less so call connect only set default destination path and can be called many times
socket.connect("/tmp/socket1.sock");
// Send can be called without path if 'connect' was called before
socket.send("I will be send to default path, set by connect!");
// CLose socket to prevent further communication
socket.close();
```
#### Operating with abstract namespace path
Abstract namespace path can be passed by starting path string from null byte ('\0') or "@" character.
```typescript
// Bind socket to abstract path
socket.bind("@/abstract/path/socket1.sock");
// Send data to abstract namespace path
socket.send("Special inter-process delivery!", "@/abstract/path/socket1.sock");
```
## Additional information
#### Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
#### License
[GPL-3.0](https://choosealicense.com/licenses/gpl-3.0/)
#### References
- http://man7.org/linux/man-pages/man7/unix.7.html
- http://man7.org/linux/man-pages/man2/socket.2.html