https://github.com/akryum/meteor-socket-io
Simple meteor example with socket.io
https://github.com/akryum/meteor-socket-io
Last synced: over 1 year ago
JSON representation
Simple meteor example with socket.io
- Host: GitHub
- URL: https://github.com/akryum/meteor-socket-io
- Owner: Akryum
- Created: 2016-07-01T07:41:23.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-01T08:42:25.000Z (about 10 years ago)
- Last Synced: 2025-02-28T18:56:45.404Z (over 1 year ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 25
- Watchers: 4
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple meteor example with socket.io
Using meteor 1.3.4.1
## Install npm packages
In your meteor project:
meteor npm install --save-dev meteor-node-stubs socket.io socket.io-client
## Server
In your server, use the 'socket.io' package:
```javascript
import http from 'http';
import socket_io from 'socket.io';
const PORT = 8080;
Meteor.startup(() => {
// Server
const server = http.createServer();
const io = socket_io(server);
let counter = 0;
// New client
io.on('connection', function(socket) {
console.log('new socket client');
});
// Start server
try {
server.listen(PORT);
} catch (e) {
console.error(e);
}
});
```
## Client
In your client, use the 'socket.io-client' package:
```javascript
// Hack https://github.com/socketio/socket.io-client/issues/961
import Response from 'meteor-node-stubs/node_modules/http-browserify/lib/response';
if (!Response.prototype.setEncoding) {
Response.prototype.setEncoding = function(encoding) {
// do nothing
}
}
// Socket io client
const PORT = 8080;
let socket = require('socket.io-client')(`http://localhost:${PORT}`);
socket.on('connect', function() {
console.log('Client connected');
});
socket.on('disconnect', function() {
console.log('Client disconnected');
});
```