transmit

Simple and fast websocket abstractions for server & client

npm install transmit

Transmit is two things:

  • Abstractions on top of the ws library
  • A similar set of abstractions for the client, automatically exposed
 
var express = require('express')
,   app = express.createServer()
 
var lactate = require('lactate').Lactate({
    expires:'two days'
})
 
app.get('/', function(reqres) {
  return lactate.serve('index.html', req, res)
})
 
app.listen(8080)
 
var transmit = require('transmit')()
 
transmit.on('connection', function(socket) {
 
  socket.on('someEvent', function(data) {
 
    socket.send('someResponseEvent', {
        success:true
    })
 
  })
 
})
 
transmit.listen(app)
 
 
<!doctype html>
 
<html>
 
  <head>
 
    <title>Transmit example</title>
    <script src='/transmit'></script> 
 
  </head>
 
  <body>
 
    <script>
 
      transmit.once('open', function() {
 
          transmit.on('someResponseEvent', function(data) {
            console.log('someResponseEvent!', data)
            //{success:true} 
          })
 
          transmit.send('someEvent', ':|')
      })
 
      transmit.connect('localhost:8080')
 
    </script> 
 
  </body>
 
</html>
 

To set ws options, you may pass them to the initialization function like so

var transmit = require('transmit')({port:2112})

You can find all of the available options here along with one exception: client_script. This option represents the name with which to expose the client script, by default, as shown in the above example, transmit.

var id = 'to_defy_the_laws_of_tradition_is_a_crusade_only_of_the_brave'
 
var transmit = require('transmit')({
    'client_script':id
})
 
/*
    The script can now be accessed from 
    /to_defy_the_laws_of_tradition_is_a_crusade_only_of_the_brave
*/