Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple and fast websocket server written in Crystal

License

NotificationsYou must be signed in to change notification settings

alternatelabs/bifrost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bifröst is a standalone websocket server written in Crystal. It’s easy to use and works with any server side language that can useJSON Web Tokens.

Build Status

Why use Bifröst?

Tools likeActionCable seamlessly integrate websockets into your web framework but can put stress on your web servers and consume a lot of memory, making your application harder to deploy and scale.

Crystal delivers blazing fast performance with minimal memory footprint so Bifröst can handle thousands of websocket connections on a tiny VPS or hobby dyno on heroku.

Quickstart

Get started by deploying this service to heroku.

Deploy

Usage

Bifrost is powered byJWTs, you can use the JWT library for the language of your choice, the examples will be inRuby.

Make sure your server side JWT secret is shared with your bifrost server to validate JWTs.

1. Create an API endpoint in your application that can give users a realtime token

If you use Ruby we have abifrost-client gem available to help simplify things.

Create a JWT that can be sent to the client side for your end user to connect to the websocket with. This should list all of the channels that user is allowed to subscribe to.

get"/api/bifrost-token"doauthenticate_user!payload={channels:["user:#{current_user.id}","global"]}jwt=JWT.encode(payload,ENV["JWT_SECRET"],"HS512"){token:jwt}.to_jsonend

2. Subscribe clients to channels

On the client side open up a websocket and send an authentication message with the generated JWT, this will subscribe the user to the allowed channels.

// Recommend using ReconnectingWebSocket to automatically reconnect websockets if you deploy the server or have any network disconnectionsimportReconnectingWebSocketfrom"reconnectingwebsocket";letws=newReconnectingWebSocket(`${process.env.BIFROST_WSS_URL}/subscribe`);// URL your bifrost server is running on// Step 1// ======// When you first open the websocket the goal is to request a signed realtime// token from your server side application and then authenticate with bifrost,// subscribing your user to the channels your server side app allows them to// connect tows.onopen=function(){axios.get("/api/bifrost-token").then((resp)=>{constjwtToken=resp.data.token;constmsg={event:"authenticate",data:jwtToken,// Your server generated token with allowed channels};ws.send(JSON.stringify(msg));console.log("WS Connected");});};// Step 2// ======// Upon receiving a message you can check the event name and ignore subscribed// and pong events, everything else will be an event sent by your server side// app.ws.onmessage=function(event){constmsg=JSON.parse(event.data);switch(msg.event){case"subscribed":{constchannelName=JSON.parse(msg.data).channel;console.log(`Subscribed to channel${channelName}`);break;}default:{// Note:// We advise you broadcast messages with a data keyconsteventData=JSON.parse(msg.data);console.log(`Bifrost msg:${msg.event}`,eventData);if(msg.event==="new_item"){console.log("new item!",eventData);}}}};// Step 3// ======// Do some cleanup when the socket closesws.onclose=function(event){console.error("WS Closed",event);};

3. Broadcast messages from the server

Generate a token and send it to bifrost

data={channel:"user:1",# Channel to broadcast tomessage:{event:"new_item",data:JSON.dump(item)},exp:Time.zone.now.to_i +1.hour}jwt=JWT.encode(data,ENV["JWT_SECRET"],"HS512")url=ENV.fetch("BIFROST_URL")url +="/broadcast"req=HTTP.post(url,json:{token:jwt})ifreq.status >206raise"Error communicating with Bifrost server on URL:#{url}"end

You're done 🚀

That's all you need to start broadcasting realtime events directly to clients in an authenticated manner. Despite the name, there is no planned support for bi-directional communication, it adds a lot of complications and for most apps it's simply not necessary.

Ping Pong 🏓

Bifröst server will send a ping to each socket every 15 seconds, if a pong hasn't been received after a further 15 seconds the socket will be closed.

GET /info.json

An endpoint that returns basic stats. As all sockets are persisted in memory if you restart the server or deploy an update the stats will reset.

{"stats":{"deliveries":117,"connected":21  }}

Contributing

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

You need to havecrystal lang installed

brew install crystal-lang

Running locally

Create a.env file in the root of this repository with the following environment variables, or set the variables if deploying to heroku.

JWT_SECRET=[> 64 character string]

Sentry is used to run the app and recompile when files change

./sentry

Running the tests

crystal spec

Built With

Versioning

We useSemVer for versioning. For the versions available, see thetags on this repository.

Authors

See also the list ofcontributors who participated in this project.

License

This project is licensed under the MIT License - see theLICENSE.md file for details

About

Simple and fast websocket server written in Crystal

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp