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.
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!');
});
server.on('connection', function(connection) {
console.log('connection recieved');
});
server.on('message', function(context) {
console.log('got a message', context.message)
});
server.on('message', 'tweet', function(context) {
console.log('got a tweet', context.message);
});
server.on('request', function(context) {
console.log('got a request', context.message);
context.reply({ answer : 'steve'});
});
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);
});
});
checkout our wiki!