@snap-net/server
v1.0.0
Published
WebSocket server for the Snap!Net networking extension.
Maintainers
Readme
snap-net / server
WebSocket server for the Snap!Net networking extension. It implements the Snap!Net wire protocol (v1.0), tracking connected peers by nickname and relaying broadcast and directed messages between them.
The server can run in two modes:
- Embedded --- instantiated inside the Electron main process of the Snap!Out desktop application, started on demand by the
start server on portblock. - Standalone --- run as a separate Node.js process for classroom-server or internet deployments.
The server code is identical in both modes.
The implemented wire protocol is documented at https://gitlab.com/snap-net/protocol
Standalone usage
# default port 3030
npx @snap-net/server
# custom port
PORT=4000 npx @snap-net/serverOr install globally:
npm install -g @snap-net/server
snap-net-serverEmbedded usage
const Server = require('@snap-net/server');
const server = new Server({
port: 3030,
onListening() {
console.log(`listening on port ${server.port}`);
},
onError(err) {
console.error(err.message);
},
});
// later
await server.close();Constructor options
| Option | Type | Description |
|--------|------|-------------|
| port | number | Port to listen on. Use 0 for a random available port. |
| onListening | function | Called when the server is ready to accept connections. |
| onError | function(err) | Called on server error (e.g. port already in use). |
Properties
| Property | Description |
|----------|-------------|
| server.port | The port the server is listening on (useful when 0 was passed). |
Methods
| Method | Description |
|--------|-------------|
| server.close() | Gracefully disconnect all clients and shut down. Returns a Promise. |
Development
npm install
npm test
# manual testing with wscat or a browser console
node dev/harness.jsRequires Node.js >= 22.
License
The Snap!Net server is free software. You may redistribute and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
