Coder Social home page Coder Social logo

simple-websocket's Introduction

simple-websocket travis npm

Simple, EventEmitter API for WebSockets

Sauce Test Status

features

  • super simple API for working with WebSockets in the browser
  • supports text and binary data
  • node.js duplex stream interface

This module works in the browser with browserify, and it's used by WebTorrent!

install

npm install simple-websocket

usage

var SimpleWebsocket = require('simple-websocket')

var socket = new SimpleWebsocket('ws://echo.websocket.org')
socket.on('connect', function () {
  // socket is connected!
  socket.send('sup!')
})

socket.on('data', function (data) {
  console.log('got message: ' + data)
})

Note: If you're NOT using browserify, then use the standalone simplewebsocket.min.js file included in this repo. This exports a SimpleWebsocket function on the window.

api

socket = new SimpleWebsocket([opts])

Create a new WebSocket connection.

If opts is specified, then it will be passed through to the underlying superclass, stream.Duplex.

socket.send(data)

Send text/binary data to the WebSocket server. data can be any of several types: String, Buffer (see buffer), TypedArrayView (Uint8Array, etc.), ArrayBuffer, or Blob (in browsers that support it).

Note: If this method is called before the socket.on('connect') event has fired, then data will be buffered.

socket.destroy([onclose])

Destroy and cleanup this websocket connection.

If the optional onclose paramter is passed, then it will be registered as a listener on the 'close' event.

Socket.WEBSOCKET_SUPPORT

Detect WebSocket support in the javascript environment.

var Socket = require('simple-websocket')

if (Socket.WEBSOCKET_SUPPORT) {
  // websocket support!
} else {
  // fallback
}

events

socket.on('connect', function () {})

Fired when the websocket connection is ready to use.

socket.on('data', function (data) {})

Received a message from the websocket server.

data will be either a String or a Buffer/Uint8Array (see buffer). JSON strings will be parsed and the resulting Object emitted.

socket.on('close', function () {})

Called when the websocket connection has closed.

socket.on('error', function (err) {})

err is an Error object.

Fired when a fatal error occurs.

real-world applications that use simple-websocket

  • StudyNotes - Helping students learn faster and better
  • instant.io - Secure, anonymous, streaming file transfer
  • lxjs-chat - Omegle chat clone
  • [ your application here - send a PR ]

license

MIT. Copyright (c) Feross Aboukhadijeh.

simple-websocket's People

Contributors

feross avatar greenkeeperio-bot avatar

Watchers

Yoann Ciabaud avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.