Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/WebReflection/pocket.io

A minimalistic version of socket.io that weights about 1K instead of 60K.
https://github.com/WebReflection/pocket.io

Last synced: about 2 months ago
JSON representation

A minimalistic version of socket.io that weights about 1K instead of 60K.

Awesome Lists containing this project

README

        

pocket.io
=========

A minimalistic version of [socket.io](https://socket.io) that weights about 1K instead of 60K.

```html

$(function () {
var socket = io();
socket.on('connect', function () {
console.log(socket.id);
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
});

```

On the Node.js side, you do the same you are doing now.

Try `node test/chat-express.js` and visit `localhost:3000` to see the classic chat demo working.

```js
var app = require('express')();
var http = require('http').Server(app);
var io = require('pocket.io')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/chat.html');
});

io.on('connection', function(socket){
console.log('a user connected');
socket.on('chat message', function (msg) {
io.emit('chat message', msg);
});
socket.on('disconnect', function () {
console.log('disconnected');
});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});
```

### API differences

You can specify a JSON like parser through the option.

As example, this is how you'd use recursion compatible serialization via the [flatted](https://github.com/WebReflection/flatted#flatted) module.

```js
// Node.js
io(server, {JSON: require('flatted/cjs')});

// client, after having Flatted exposed somehow, i.e.
//
io({JSON: Flatted});
```