Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sebyx07/opal-express-socketio
Opal Ruby + Express + Socket.io
https://github.com/sebyx07/opal-express-socketio
express nodejs opal realtime ruby socketio websockets
Last synced: 3 days ago
JSON representation
Opal Ruby + Express + Socket.io
- Host: GitHub
- URL: https://github.com/sebyx07/opal-express-socketio
- Owner: sebyx07
- License: mit
- Created: 2019-10-20T13:46:16.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-11T10:06:49.000Z (almost 2 years ago)
- Last Synced: 2024-08-02T11:23:48.105Z (3 months ago)
- Topics: express, nodejs, opal, realtime, ruby, socketio, websockets
- Language: Ruby
- Size: 1010 KB
- Stars: 22
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: readme.MD
- License: LICENSE
Awesome Lists containing this project
- awesome-opal - Opal Node server with Express.js and Socket.io Demo Chat App - Project which uses Opal (Ruby) with a Node.js server, Express and Socket.io to build a real time chat (Uncategorized / Uncategorized)
README
# Opal implementation of a simple server with Express and Socket.io
Demo https://opal-realtime-socket-express.herokuapp.com/
### Run
Installation
```bash
git clone https://github.com/sebyx07/opal-express-socketio.git opal-server
cd opal-server
bundle install && yarn install # install deps
```Update index.html
```js
var socket = io.connect('https://opal-realtime-socket-express.herokuapp.com', {transports:['websocket']});
// with
var socket = io.connect('http://localhost:8000', {transports:['websocket']});
```### Usage
`rake` it's the default rake task that starts the server
go to http://localhost:8000
```ruby
Server.new(8000) do |s, io|
s # you can access express methods: with get, post, use, etc. Most is supported
# callbacks, you replace them with do; end blocksio # you can use socket-io methods: on, emit etc. Most is supported
# socket must be wrapped using Native()
end
```### Example server usage
```ruby
Server.new(8000) do |s, io|
# express part
s.use(Logger) # use middlewares
s.start! # start the servers.get("/") do |_, res| # handle get to root request
res.sendFile(s.view("index.html")) # send the index html file
ends.get("/:name") do |req, res| # handle dynamic requests
res.send(req.params.name)
end# socket io part
io.on 'connection' do |socket| # initialize socket io connection
socket = Native(socket) # if you have problems with apply for a object, just Native() itsocket.on('chat message') do |msg| # listen for messages
io.emit('chat message', msg) # send it back, to all clients
endsocket.on('disconnect') do # handle disconnect
p "disconnect"
end
end
end
```