wyre

1.2.1 • Public • Published

Build Status Join the chat at https://gitter.im/from-nibly/wyre

wyre

wyre is a websocket library that will change the way you use websockets. Wyre allows you to send messages that require a response message to be sent. Wyre also allows you to filter messages based on the type of message which you specify.

Getting Started

Server

var Server = require('wyre').Server;
var server = new Server();
 
//here we start the server
server.listen({ port : 1234}, function(err) {
    console.log('server is started!');
});

here we can listen for new connections

server.on('connection', function(connection) {
    console.log('connection recieved');
});

here we can listen for incoming messages

server.on('message', function(context) {
    console.log('got a message', context.message)
});

here we can listen for messages where the message type is a tweet

server.on('message', 'tweet', function(context) {
    console.log('got a tweet', context.message);
});

here we can listen for a request and reply to it

server.on('request', function(context) {
  console.log('got a request', context.message);
  context.reply({ answer : 'steve'});
});

Client

Clients are very similar but you connect to a server differently

var Client = require('wyre').Client;
var client = new Client();
 
//here we start the server
client.connect('ws://localhost:1234', function(err, connection) {
  if(err) {
    console.log('error connecting to the server', err);
  }
  else {
    console.log('client connected to server', connection);
  }
  //here we can send a message or a request;
  connection.send({ myNameIs : "bob" });
  connection.request({ question : "what is your name?"}, function(err, reply) {
    console.log('servers name is', reply.message.answer);
  });
});

Ready for more?

checkout our wiki!

or checkout our gitter channel for questions Join the chat at https://gitter.im/from-nibly/wyre

Dependents (1)

Package Sidebar

Install

npm i wyre

Weekly Downloads

1

Version

1.2.1

License

MIT

Last publish

Collaborators

  • from-nibly