├── .gitignore
├── LICENSE
├── README.md
├── browser.js
├── demos
├── nodejs
│ └── server.js
└── web
│ └── index.html
├── index.js
├── lib
└── wscb.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Aid Vllasaliu / Antiphase AB
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # websockets-callback
2 | WebSocket messages with callbacks.
3 | There's a C++ port of this library called [WebSockets-Callback.CPP](https://github.com/aidv/WebSockets-Callback.CPP)
4 |
5 | ## NEW IN 0.4.8
6 |
7 | Now supports Electron.
8 |
9 | ### Projects using websockets-callback
10 | - [I2Catalyst](https://github.com/aidv/i2catalyst) - A browser based I2C packet analyzer using NodeJS
11 | - [jobmatch-er](https://github.com/jobmatch-er/jobmatch.er-ws) - We don't know what this project does, but thanks for using WSCB
12 |
13 |
14 | ### Introduction
15 |
16 | WSCB's goal is to be the easiest solution to communicate between devices using Websockets.
17 |
18 | It contains two main functions: `on()` and `send()`.
19 |
20 | It has two types of messages: `Expectations` and `Unexpected`.
21 |
22 | Expectations are messages that trigger an `on()` binding on the receiving end of the pipe.
23 |
24 | Unexpected messages will trigger a commonly shared function.
25 |
26 | ### Expectations
27 |
28 | An expectation callback has two arguments: `msg` and `respondWith()`.
29 |
30 | The `msg` object contains the data that the sender has sent, and `respondWith(obj)` is a function that takes an object as its argument.
31 |
32 | The function `respondWith()` is used to reply to the sender, usually used as an ACK signal.
33 |
34 | Example:
35 |
36 | ```js
37 | wscb.on('human', function(msg, respondWith){
38 | respondWith({human: 'homosapien'});
39 | });
40 | ```
41 |
42 |
43 | ### Coding fashion
44 | Firstly, it's important to understand that the file ```wscb.js``` inside the folder ```./lib``` is cross compatible with both NodeJS and the browser (in my case Chrome).
45 |
46 | To create a trigger you call the ```on(object, onHandle)``` function and pass an object (as the message) and specify a handler function.
47 | The handler function is called when the trigger is triggered.
48 | The handler function has two parameters:
49 | - The message
50 | - A response function called ```respondWith(object)``` that you call when you want to respond to the message.
51 | NOTE:
52 | If the response object contains the key ```progress```, the expectation on the other end of the pipe will not be removed
53 | and allows for several responses to be sent until you either (A) set the ```progress``` value to ```100``` or respond
54 | without the ```progress``` key.
55 |
56 |
57 | To send an expectation (*1*) you call the ```send(object, onResponse, onProgress, connection)``` and feed an object (as the message), the response handler, the progress handler and the connection to that should carry the message.
58 |
59 | It's also important to note that the connection parameter is only used in NodeJS.
60 | Why is that? Because in NodeJS you're most likely to run a server (although you can create a client too) and so when you want to send an expectation or a responseless message to a client, you also need to define who you're sending it to. Thus the connection parameter has to be defined upon calling the ```send(object, onResponse, onProgress, connection)``` function.
61 |
62 | The progress handler is only called when the key ```progress``` exists in the response message.
63 | The ```progress``` value has a range of 0 to 100 where when at 100 (or non-existant) the expectation is deleted
64 |
65 | (*1*) An "expectation" is a message that expects a response. If no response is received, the expectation will wait forever.
66 |
67 |
68 | #### Using in an Electron app
69 |
70 | In your `main.js` add the following:
71 | ```js
72 | const WebSockets_Callback = require('wscb');
73 | var wscb = new WebSockets_Callback({asElectron: true})
74 |
75 | //follow the rest of the instructions below
76 | ```
77 |
78 | In your `index.html` add the following in the body:
79 | ```js
80 |
86 | ```
87 |
88 |
89 |
90 |
91 | #### Creating a trigger (cross compatible)
92 | A trigger is triggered when a specified message is received.
93 | Once the trigger shoots and you've handled the message, you can respond to the message by calling ```respondWith()```
94 |
95 | ```js
96 | wscb.on('human',
97 | function(msg, respondWith){
98 | respondWith({human: 'homosapien'});
99 | }
100 | );
101 | ```
102 |
103 | #### Sending an expectation
104 |
105 | ```js
106 | wscb.send(
107 | {key: 'value', greeting: 'hello world!'},
108 | function(response){
109 |
110 | },
111 | function(response){
112 | console.log(response.progress + '% done');
113 | },
114 | conn //set to undefined or ignore if sending from the browser
115 | );
116 | ```
117 |
118 | ### To install from npm:
119 | ```
120 | npm i wscb
121 | ```
122 |
123 | ### Options:
124 | ```js
125 | var options = {
126 | verbose: false, //will log some messages to the console
127 | asClient: false, //will setup WSCB as a client. Browser incompatible.
128 | asElectron: false, //will use Electron IPC instead of Websockets
129 |
130 | address: '127.0.0.1',
131 | port: 8081,
132 | onOpen: undefined,
133 | onError: undefined,
134 | onListening: undefined,
135 | onUnexpectedMessage: undefined
136 | }
137 | ```
138 |
139 | ### NodeJS sample code:
140 | ```js
141 | const WebSockets_Callback = require('wscb');
142 |
143 |
144 | var options = {}
145 |
146 | var wscb = new WebSockets_Callback(options);
147 |
148 | wscb.on('hello from client :)', function(msg, respondWith){
149 | console.log('Client said:')
150 | console.log(msg)
151 | respondWith({msg: 'hi from server :D'});
152 | })
153 |
154 | wscb.on('waitFor', function(msg, respondWith){
155 | setTimeout(() => {
156 | respondWith({msg: 'Delayed for ' + msg.delay + ' ms'});
157 | }, msg.delay);
158 |
159 | })
160 |
161 | wscb.on('progress', function(msg, respondWith){
162 | var progress = -1;
163 | var progressTimer = setInterval(() => {
164 | progress++;
165 | if (progress >= 100){
166 | progress = 100;
167 | clearInterval(progressTimer);
168 | }
169 | respondWith({progress: progress});
170 | }, 10);
171 | })
172 |
173 | wscb.options.onUnexpectedMessage = function(conn, msg){
174 | console.log('Client sent a responseless message: ' + msg)
175 | }
176 |
177 |
178 | //wait for client to connect
179 | wscb.options.onOpen = function(conn){
180 | console.log('Client connected')
181 | //Send some tests to client and wait for responses
182 | console.log('Sending waitFor test (3000 ms)...')
183 | wscb.send({cmd: 'waitFor', delay: 3000},
184 | function(response){
185 | console.log(response.msg);
186 |
187 | setTimeout(progressTest, 2000);
188 | },
189 | undefined, //not expecting any progress
190 | conn
191 | )
192 |
193 | function progressTest(){
194 | wscb.send({cmd: 'progress'},
195 | function(response){
196 | console.log('Progress test completed!');
197 | },
198 | function(response){
199 | console.log(response.progress + '% done');
200 | },
201 | conn
202 | )
203 | }
204 | }
205 | ```
206 |
207 | ### Browser sample code:
208 | ```html
209 |
210 |