@superhero/socket

TCP socket connection, client and server

Usage no npm install needed!

<script type="module">
  import superheroSocket from 'https://cdn.skypack.dev/@superhero/socket';
</script>

README

Socket

Licence: MIT


npm version

A socket connection for "server to server" communication.

Install

npm install @superhero/socket

...or just set the dependency in your package.json file:

{
  "dependencies":
  {
    "@superhero/socket": "*"
  }
}

Example Application

A simple example to get started follows.

const
SocketFactory = require('@superhero/socket'),
socketFactory = new SocketFactory,
// Debug      = require('@superhero/debug'),
// log        = new Debug({ debug:true }),
log           = console,
server        = socket.createServer(log),
client        = socket.createClient(log),
port          = 18200,
event         = 'foobar',
body          = { foo:'bar' }

server.listen(port)
client.connect(port)

client.emit(event, body)
server.on(event, (context, data) => context.emit(event, body))
client.on(event, (context, data) =>
{
  // if you need to close the connection, then...
  client.client.end()
  server.server.close()
})