https://github.com/sleep2death/protobuf.io
protocol.io is a simple socket server which sending and recieving data with protocol buffers.
https://github.com/sleep2death/protobuf.io
io nodejs protobuf protocol-buffers socket
Last synced: 10 days ago
JSON representation
protocol.io is a simple socket server which sending and recieving data with protocol buffers.
- Host: GitHub
- URL: https://github.com/sleep2death/protobuf.io
- Owner: sleep2death
- Created: 2018-09-25T15:10:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-27T14:47:31.000Z (over 7 years ago)
- Last Synced: 2025-11-27T09:34:40.780Z (2 months ago)
- Topics: io, nodejs, protobuf, protocol-buffers, socket
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# protobuf.io
[](https://github.com/sleep2death/protobuf.io)
[](https://travis-ci.org/sleep2death/protobuf.io)
**protocol.io** is a simple socket server which sending and recieving data with [protocol buffers](https://developers.google.com/protocol-buffers/).The project is inspired by [socket.io](https://github.com/socketio/socket.io/)
### Installation
`npm install protobuf.io --save` or `yarn add protobuf.io`
### How to use
```javascript
// create the new server with options,
// port is the listening port, default is 3000
// protocolPath is your proto file path, default is './proto/main.proto'
const protobuf = require('protobuf.io')
var server = new protobuf.Server({ port: 3000, protocolPath: './proto/main.proto' })
// server error handling
server.on('error', error => {
console.error(error)
})
// when server start listening
server.on('listening', () => {
console.log('Listening port:', server.port)
// now create a transport to decode the message from server
var transport = new protobuf.Transport()
transport.loadProtocol('./proto/main.proto').then(transport => {
// connect to the server
var socket = require('net').createConnection(3000)
socket.on('data', data => {
// using transport to recieve and decode the message
transport.recieve(socket, data, (socket, index, buffer) => {
var msg = transport.decode(index, buffer)
console.log('recieved a msg from server:', msg)
var payload = transport.encode('ping', { index: 1 })
transport.send(socket, payload)
console.log('now send a ping with index:', 1)
})
})
})
})
server.on('connection', client => {
console.log('a client connected:', client.id)
// send a handshake message to client
client.send('handshake', { session: 'thisisasession' })
console.log('server send a handhsake message to client:', { session: 'this is a session' })
client.on('message', msg => {
// got a message from client
console.log('recieved a message from client:', msg)
})
})
// start the server
server.start()
```
STILL UNDER CONSTRUCTION...