npm.io
0.2.0 • Published 7 years ago

gohub

Licence
MIT
Version
0.2.0
Deps
0
Size
16 kB
Vulns
0
Weekly
0

Go Hub (JS Client)

This is the client library for Go Hub. It aims to simplify the use of the topic and room concepts that exist in the Go Hub library.

Basic Usage

This library is compatible with both Common JS and ES6 modules. The examples shown here are using the Common JS library version. However, they can easily be changed to work with ES6 modules.

let sock = new Socket('ws://localhost:3000/ws');

// When the channel is opened join rooms (this will be called with reconnects too)
sock.onOpen(e => {
  // Connect to the channel
  const channel = sock.join('chatroom:334');

  // On event 'message_received' from the server log the event and data
  channel.onEvent('message_received', (event, payload) => {
    console.log(event, payload);
  });

  // Send a message to the server
  channel.send('send_message', 'sweet');

  // Leave the channel
  channel.leave();
});

// Connect to the WebSocket
sock.connect();

Keywords