1.0.1 • Published 10 years ago

websocket-promise v1.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
10 years ago

WebSocket-Promise

WebSocket-Promise is an extremely lightweight Promise wrapper for native HTML5 WebSockets written in JS. It provides an easy to use API on both the Browser and Server Side.

WebSocket-Promise internally uses Le-Emitter for the Browser-Sided event emitting part, You can replace it with an EventEmitter of your choice.

Hello World

Node

var WebSocketP = require('websocket-promise')
var Server = new WebSocketP({port: 8090})
Server.onConnection(function(Client){
  Client.on('Hello', function(Job){
    console.log(Job.Message) // "World"
    Job.Response = 'World'
  })
})

Browser

var Connection = new WebSocketP("ws://localhost:8090")
Connection.Request("Hello", "World").then(function(Response){
  console.log(Response) // "World"
})

API

enum JobType = {Broadcast, Reply, Request}
type Job = shape(Type => JobType, SubType => string, Message => Mixed, ?ID => String, EXCHANGE => true)
class Server{
  constructor(Options: Object) // Available Options: https://github.com/websockets/ws/blob/master/lib/WebSocketServer.js#L25
  onConnection(Callback: Function<ServerClient>): void
  Broadcast(Type: String, Message: Any): void
}
class ServerClient{
  on(Type: String, Callback: Function): Disposable
  request(Type: String, Message): Promise
  terminate(): void
}
class WebSocketP{ // Available under same name in browser
  on(Type: String, Callback: Function): Disposable
  request(Type: String, Message): Promise
  terminate(): void
}

License

This project is licensed under the terms of MIT License. See the License file for more info.

1.0.1

10 years ago

1.0.0

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago